日期:2014-05-16 浏览次数:20404 次
var cookie_pre = 'sYQDUGqqzH'; var cookie_domain = ''; var cookie_path = '/'; function getcookie(name) { name = cookie_pre+name; var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while(i < clen) { var j = i + alen; if(document.cookie.substring(i, j) == arg) return getcookieval(j); i = document.cookie.indexOf(" ", i) + 1; if(i == 0) break; } return null; } function setcookie(name, value, days) { name = cookie_pre+name; var argc = setcookie.arguments.length; var argv = setcookie.arguments; var secure = (argc > 5) ? argv[5] : false; var expire = new Date(); if(days==null || days==0) days=1; expire.setTime(expire.getTime() + 3600000*24*days); document.cookie = name + "=" + escape(value) + ("; path=" + cookie_path) + ((cookie_domain == '') ? "" : ("; domain=" + cookie_domain)) + ((secure == true) ? "; secure" : "") + ";expires="+expire.toGMTString(); } function delcookie(name) { var exp = new Date(); exp.setTime (exp.getTime() - 1); var cval = getcookie(name); name = cookie_pre+name; document.cookie = name+"="+cval+";expires="+exp.toGMTString(); } function getcookieval(offset) { var endstr = document.cookie.indexOf (";", offset); if(endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" class="addbg"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>phpcmsV9 - 后台管理中心</title> <script type="text/javascript" src="cookie.js"></script> <script> alert(getcookie('mytest')); function add(name,v){ setcookie(name,v,1); } function dle(name){ delcookie(name,v,1); } </script> </head> <body> <input type="button" value="addcookie" onclick="add('mytest','aaaa')" /> <input type="button" value="dlecookie" onclick="dle('myuser');" /> </body> </html>