写入common.js文件,其他页面调用即可:
//添加cookie值function addcookie(name, value, days) { days = days || 0; var expires = ""; if (days != 0) var date = new Date(); date.setTime(date.getTime() + (days * 24 * 3600 * 1000)); expires = "; expires=" + date.toGMTString(); } document.cookie = name + "=" + escape(value) + expires + "; path=/";}//获取cookie值function getcookie(name) { var strcookie = document.cookie; var arrcookie = strcookie.split("; "); for (var i = 0; i < arrcookie.length; i++) { var arr = arrcookie[i].split("="); if (arr[0] == name) return arr[1]; } return "";}//删除cookie值function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval = getcookie(name); if (cval != null) document.cookie = name + "=" + cval + "; path=/;expires=" + exp.toGMTString();}
例子:
//添加一个名为name的cookie值addcookie("name","123456456");//获取一个名为name的cookie值var name = getcookie("name");//获取页面id名为cookie的标签var text = document.getElementById("cookie");//将页面id名为cookie的标签的innerHTML的内容改为name所对应的值text.innerHTML=name; //延时五秒后 删除一个名为name的cookie值setTimeout(function(){ delCookie("name");},5000);