一、JS 对cookie 操作
function
setCookie(name,value)
... {
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString();
}
function getCookie(name)
... {
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}
function delCookie(name)
... {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie=name +"="+cval+";expires="+exp.toGMTString();
}
... {
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString();
}
function getCookie(name)
... {
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}
function delCookie(name)
... {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie=name +"="+cval+";expires="+exp.toGMTString();
}
2、js 获取页面的参数
3、JS 异步装载Xml文件
<
script language
=
"
JavaScript
"
>
function getUrlParameter(asName) ... {
var lsURL=window.location.href;
loU = lsURL.split("?");
if (loU.length>1)...{
var loallPm = loU[1].split("&");
for (var i=0; i<loallPm.length; i++)
...{
var loPm = loallPm[i].split("=");
if (loPm[0]==asName)...{
if (loPm.length>1)...{
return loPm[1];
}else...{
return "";
}
}
}
}
return null;
}
alert(getUrlParameter( " id " ));
</ script >
function getUrlParameter(asName) ... {
var lsURL=window.location.href;
loU = lsURL.split("?");
if (loU.length>1)...{
var loallPm = loU[1].split("&");
for (var i=0; i<loallPm.length; i++)
...{
var loPm = loallPm[i].split("=");
if (loPm[0]==asName)...{
if (loPm.length>1)...{
return loPm[1];
}else...{
return "";
}
}
}
}
return null;
}
alert(getUrlParameter( " id " ));
</ script >
<
script
>
var xmldoc;
function Load() ... {
xmldoc = new ActiveXObject("Msxml2.DOMDocument");
xmldoc.onreadystatechange = CheckState;
xmldoc.resolveExternals = false;
xmldoc.load(URL.value);
}
function CheckState() ... {
var state = xmldoc.readyState;
RESULTS.innerHTML += "加载状态 = " + state + "<BR>"
if (state == 4)...{
var err = xmldoc.parseError;
if (err.errorCode != 0)
RESULTS.innerHTML += err.reason + "<BR>"
else
...{
RESULTS.innerHTML += "加载成功。" + "<BR>"
alert(xmldoc.xml)
}
}
}
</ script >
var xmldoc;
function Load() ... {
xmldoc = new ActiveXObject("Msxml2.DOMDocument");
xmldoc.onreadystatechange = CheckState;
xmldoc.resolveExternals = false;
xmldoc.load(URL.value);
}
function CheckState() ... {
var state = xmldoc.readyState;
RESULTS.innerHTML += "加载状态 = " + state + "<BR>"
if (state == 4)...{
var err = xmldoc.parseError;
if (err.errorCode != 0)
RESULTS.innerHTML += err.reason + "<BR>"
else
...{
RESULTS.innerHTML += "加载成功。" + "<BR>"
alert(xmldoc.xml)
}
}
}
</ script >
4、预判断一下链接URL是否可用5、保护页面不被打印、复制,禁止Mouse各键
<
script language
=
"
javascript
"
>
function getURL(url)
... {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",url,false);
xmlhttp.send();
if (xmlhttp.readyState==4)
alert((xmlhttp.Status==200)?"存在":"不存在");
}
// user like this:
getURL( " http://localhost/1.asp " )
</ script >
function getURL(url)
... {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",url,false);
xmlhttp.send();
if (xmlhttp.readyState==4)
alert((xmlhttp.Status==200)?"存在":"不存在");
}
// user like this:
getURL( " http://localhost/1.asp " )
</ script >
<
SCRIPT LANGUAGE
=
"
JavaScript
"
>
wside
=
(window.sidebar)
?
true
:
false
;
var w456 = false ;
function nrcie() ... {return false;} ;
function nrcns(e) ... { if(e.which==2||e.which==3) return false;
} if (document.layers) ... { document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=nrcns;
} document.oncontextmenu = nrcie;
document.onselectstart = new Function( " return false " );
function nsel() ... {if((document.layers)||wside)...{var qwe= document.getSelection();
if(qwe!="") ...{window.find(" ")};
setTimeout("nsel()",20);
}} nsel();
function ccd() ... {clipboardData.clearData();
} ;
function cce() ... {ccd();
setTimeout("cce()",30);
} ;
cce();
</ SCRIPT >
< style media = ' print ' > body ... {display:none} </ style >
var w456 = false ;
function nrcie() ... {return false;} ;
function nrcns(e) ... { if(e.which==2||e.which==3) return false;
} if (document.layers) ... { document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=nrcns;
} document.oncontextmenu = nrcie;
document.onselectstart = new Function( " return false " );
function nsel() ... {if((document.layers)||wside)...{var qwe= document.getSelection();
if(qwe!="") ...{window.find(" ")};
setTimeout("nsel()",20);
}} nsel();
function ccd() ... {clipboardData.clearData();
} ;
function cce() ... {ccd();
setTimeout("cce()",30);
} ;
cce();
</ SCRIPT >
< style media = ' print ' > body ... {display:none} </ style >
6、email的判断。
function
ismail(mail)
... {
return(new RegExp(/^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)
[A-Za-z0-9]+)*.[A-Za-z0-9]+$/).test(mail));
}
... {
return(new RegExp(/^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)
[A-Za-z0-9]+)*.[A-Za-z0-9]+$/).test(mail));
}
7、身份证的验证
function
isIdCardNo(num)
... {
if (isNaN(num)) ...{alert("输入的不是数字!"); return false;}
var len = num.length, re;
if (len == 15)
re = new RegExp(/^(d...{6})()?(d...{2})(d...{2})(d...{2})(d...{3})$/);
else if (len == 18)
re = new RegExp(/^(d...{6})()?(d...{4})(d...{2})(d...{2})(d...{3})(d)$/);
else ...{alert("输入的数字位数不对!"); return false;}
var a = num.match(re);
if (a != null)
...{
if (len==15)
...{
var D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&
D.getDate()==a[5];
}
else
...{
var D = new Date(a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&
D.getDate()==a[5];
}
if (!B) ...{alert("输入的身份证号 "+ a[0] +" 里出生日期不对!"); return false;}
}
return true;
}
... {
if (isNaN(num)) ...{alert("输入的不是数字!"); return false;}
var len = num.length, re;
if (len == 15)
re = new RegExp(/^(d...{6})()?(d...{2})(d...{2})(d...{2})(d...{3})$/);
else if (len == 18)
re = new RegExp(/^(d...{6})()?(d...{4})(d...{2})(d...{2})(d...{3})(d)$/);
else ...{alert("输入的数字位数不对!"); return false;}
var a = num.match(re);
if (a != null)
...{
if (len==15)
...{
var D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&
D.getDate()==a[5];
}
else
...{
var D = new Date(a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&
D.getDate()==a[5];
}
if (!B) ...{alert("输入的身份证号 "+ a[0] +" 里出生日期不对!"); return false;}
}
return true;
}
8、判断上传文件的类型
<input type=file οnchange="alert(this.value.match(/^(.*)(/.)(.{1,8})$/)[3])">
9、不断地清空剪贴板
<body οnlοad="setInterval('clipboardData.setData(/'Text/',/'/')',100)">
<script language="javascript" type="text/javascript">
file://先复制一样东西,或者文本或者图片
if(clipboardData.getData("Text")||clipboardData.getData("HTML")||
clipboardData.getData("URL"))
{
alert("有效行为");
}
</script>
10、JS调用WebService
<
script language
=
"
javascript
"
>
// Test function with get method.
function RequestByGet(data) ... {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice location.
var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";
xmlhttp.Open("GET",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
var result = xmlhttp.status;
//OK
if(result==200) ...{
document.write(xmlhttp.responseText);
}
xmlhttp = null;
}
// Test function with post method
function RequestByPost(value)
... {
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
data = data + '<soap:Body>';
data = data + '<SayHelloTo xmlns="http://tempuri.org/">';
data = data + '<Name>'+value+'</Name>';
data = data + '</SayHelloTo>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL="http://localhost:1323/WebSite6/Service.asmx";
xmlhttp.Open("POST",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
document.write( xmlhttp.responseText);
}
</ Script >
// Test function with get method.
function RequestByGet(data) ... {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice location.
var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";
xmlhttp.Open("GET",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
var result = xmlhttp.status;
//OK
if(result==200) ...{
document.write(xmlhttp.responseText);
}
xmlhttp = null;
}
// Test function with post method
function RequestByPost(value)
... {
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
data = data + '<soap:Body>';
data = data + '<SayHelloTo xmlns="http://tempuri.org/">';
data = data + '<Name>'+value+'</Name>';
data = data + '</SayHelloTo>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL="http://localhost:1323/WebSite6/Service.asmx";
xmlhttp.Open("POST",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
document.write( xmlhttp.responseText);
}
</ Script >