JavaScript常用代码
页面的按钮全选:
function CheckBoxAll() {
//得到页面上所有input表单元素:document.getElementsByTagName("input")
for (var i = 0; i < document.getElementsByTagName("input").length; i++) {
var e = document.getElementsByTagName("input")[i];
//判断类型为 checkbox的表单元素
if (e.type == "checkbox") {
e.checked = document.getElementById_x("CheckAll").checked;
}
}
}
小应用:
history.back() //跟点击浏览器上的后退按钮效果一样。
history.back(1) //表示返回上一页,或后退的意思~~返回到访问过的上一个,
//前一个页面,相当于点击一次浏览器的后退按钮。
非法字符:
var pat=new RegExp("[^a-zA-Z0-9\_\u4e00-\u9fa5]","i");
if(pat.test(una.value)==true|| pat.test(psw.value)==true)
{
if(pat.test(una.value)==true|| pat.test(psw.value)==true)
{
alert('输入含有非法字符!');
return false;
return false;
}
else
else
{
return true;
}
电子邮箱:
function CheckExists()
{
var e = document.getElementById_xx("mailaddress").value;
if(e != "") {
if(!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(e))
{
alert("请输入格式正确的 e-mail 地址!");
var email = document.getElementById_xx ( "mailaddress" );
email.value = "";
email.focus ();
}
else
{
QuickRegisterBox.IsEMailExists(e, CheckExists_Callback);
}
}
}
{
var e = document.getElementById_xx("mailaddress").value;
if(e != "") {
if(!/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(e))
{
alert("请输入格式正确的 e-mail 地址!");
var email = document.getElementById_xx ( "mailaddress" );
email.value = "";
email.focus ();
}
else
{
QuickRegisterBox.IsEMailExists(e, CheckExists_Callback);
}
}
}
设置与获取Cookie:
//获取Cookie
function SetCookie(name, value)//两个参数,一个是cookie的名子,一个是值
{
var Days = 1; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
//exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
exp.setTime(exp.getTime() + 5 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
var Days = 1; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
//exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
exp.setTime(exp.getTime() + 5 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
//获取Cookie
function getCookie(name) {
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null) return unescape(arr[2]); else return null;
}
function getCookie(name) {
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null) return unescape(arr[2]); else return null;
}
获取地址栏参数
//获取地址栏参数
function Request(strName) {
var strHref = window.location.search;
var intPos = strHref.indexOf("?");
var strRight = strHref.substr(intPos + 1);
var arrTmp = strRight.split("&");
for (var i = 0; i < arrTmp.length; i++) {
var arrTemp = arrTmp[i].split("=");
if (arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];
}
return "";
}
function Request(strName) {
var strHref = window.location.search;
var intPos = strHref.indexOf("?");
var strRight = strHref.substr(intPos + 1);
var arrTmp = strRight.split("&");
for (var i = 0; i < arrTmp.length; i++) {
var arrTemp = arrTmp[i].split("=");
if (arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];
}
return "";
}