操作Cookie By JavaScript:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1)
c_end=document.cookie.length;
return decodeURIComponent(document.cookie.substring(c_start,c_end));
}
}
return null;
}
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate);
}
getCookie,setCookie,delCookie:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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();
}
getElementsByClassName:
function
getElementsByClassName(searchClass, domNode, tagName) {
alert(searchClass);
if (domNode == null ) domNode = document;
if (tagName == null ) tagName = ' * ' ;
var el = new Array();
var tags = domNode.getElementsByTagName(tagName);
var tcl = " " + searchClass + " " ;
for (i = 0 ,j = 0 ; i < tags.length; i ++ ) {
var test = " " + tags[i].className + " " ;
if (test.indexOf(tcl) != - 1 )
el[j ++ ] = tags[i];
}
alert(el);
return el;
}
alert(searchClass);
if (domNode == null ) domNode = document;
if (tagName == null ) tagName = ' * ' ;
var el = new Array();
var tags = domNode.getElementsByTagName(tagName);
var tcl = " " + searchClass + " " ;
for (i = 0 ,j = 0 ; i < tags.length; i ++ ) {
var test = " " + tags[i].className + " " ;
if (test.indexOf(tcl) != - 1 )
el[j ++ ] = tags[i];
}
alert(el);
return el;
}
Event:
var
EventUtil
=
new
Object;
// oTarget:目标;sEventType:事件名称;funName:事件触发的函数名;
EventUtil.addEvent = function (oTarget,sEventType,funName){
if (oTarget.addEventListener){ // for DOM;
oTarget.addEventListener(sEventType,funName, false );
} else if (oTarget.attachEvent){
oTarget.attachEvent( " on " + sEventType,funName);
} else {
oTarget[ " on " + sEventType] = funName;
}
};
EventUtil.removeEvent = function (oTarget,sEventType,funName){
if (oTarget.removeEventListener){ // for DOM;
oTarget.removeEventListener(sEventType,funName, false );
} else if (oTarget.detachEvent){
oTarget.detachEvent( " on " + sEventType,funName);
} else {
oTarget[ " on " + sEventType] = null ;
}
};
// oTarget:目标;sEventType:事件名称;funName:事件触发的函数名;
EventUtil.addEvent = function (oTarget,sEventType,funName){
if (oTarget.addEventListener){ // for DOM;
oTarget.addEventListener(sEventType,funName, false );
} else if (oTarget.attachEvent){
oTarget.attachEvent( " on " + sEventType,funName);
} else {
oTarget[ " on " + sEventType] = funName;
}
};
EventUtil.removeEvent = function (oTarget,sEventType,funName){
if (oTarget.removeEventListener){ // for DOM;
oTarget.removeEventListener(sEventType,funName, false );
} else if (oTarget.detachEvent){
oTarget.detachEvent( " on " + sEventType,funName);
} else {
oTarget[ " on " + sEventType] = null ;
}
};
比较实用的,不需要很多无用的代码.