<script>//写cookies函数 作者:翟振凯function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值{ 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)//取cookies函数 { var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return null;}function delCookie(name)//删除cookie{ var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=getCookie(name); if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();}SetCookie ("xiaoqi", "3")alert(getCookie('xiaoqi'));</script>
一个非常实用的Javascript读写Cookie函数一个非常实用的javascript读写Cookie函数 function GetCookieVal(offset)//获得Cookie解码后的值{var endstr = documents.cookie.indexOf (";", offset);if (endstr == -1)endstr = documents.cookie.length;return unescape(documents.cookie.substring(offset, endstr));}function SetCookie(name, value)//设定Cookie值{var expdate = new Date();var argv = SetCookie.arguments;var argc = SetCookie.arguments.length;var expires = (argc > 2) ? argv[2] : null;var path = (argc > 3) ? argv[3] : null;var domain = (argc > 4) ? argv[4] : null;var secure = (argc > 5) ? argv[5] : false;if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));documents.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))+((secure == true) ? "; secure" : "");}function DelCookie(name)//删除Cookie{var exp = new Date();exp.setTime (exp.getTime() - 1);var cval = GetCookie (name);documents.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();}function GetCookie(name)//获得Cookie的原始值{var arg = name + "=";var alen = arg.length;var clen = documents.cookie.length;var i = 0;while (i < clen){var j = i + alen;if (documents.cookie.substring(i, j) == arg)return GetCookieVal (j);i = documents.cookie.indexOf(" ", i) + 1;if (i == 0) break;}return null;} <SCRIPT language="javascript"><!--function openpopup(){url="popup.htm"window.open("gonggao.htm","gonggao","width=260,height=212,left=200,top=0")}function get_cookie(Name) {var search = Name + "="var returnvalue = "";if (documents.cookie.length > 0) {offset = documents.cookie.indexOf(search)if (offset != -1) {offset += search.lengthend = documents.cookie.indexOf(";", offset);if (end == -1)end = documents.cookie.length;returnvalue=unescape(documents.cookie.substring(offset, end))}}return returnvalue;}function helpor_net(){if (get_cookie('popped')==''){openpopup()documents.cookie="popped=yes"}}helpor_net()//--></SCRIPT> 如果点了确定,只要不清cookie,以后访问都不会再提示,如果不点确定则每次都会提示。放在js文件里,全站包含<SCRIPT LANGUAGE="JavaScript"><!--var the_cookie = document.cookie;var broken_cookie = the_cookie.split(":");var the_visiteraccepted = unescape(broken_cookie[1]);//if (the_visiteraccepted=="undefined"){ var tmp=confirm('中国人何时何地。'); if(tmp==false){ window.close(); }else{ var the_visiteraccepted = 1; var the_cookie = "ILoveChina=visiteraccepted:" + escape(the_visiteraccepted); document.cookie = the_cookie; }}//--></SCRIPT>
1. Cookie的兼容性问题 Cookie的格式有2个不同的版本,第一个版本,我们称为Cookie Version 0,是最初由Netscape公司制定的,也被几乎所有的浏览器支持。而较新的版本,Cookie Version 1,则是根据RFC 2109文档制定的。为了确保兼容性,JAVA规定,前面所提到的涉及Cookie的操作都是针对旧版本的Cookie进行的。而新版本的Cookie目前还不被Javax.servlet.http.Cookie包所支持。2. Cookie的内容 同样的Cookie的内容的字符限制针对不同的Cookie版本也有不同。在Cookie Version 0中,某些特殊的字符,例如:空格,方括号,圆括号,等于号(=),逗号,双引号,斜杠,问号,@符号,冒号,分号都不能作为Cookie的内容。这也就是为什么我们在例子中设定Cookie的内容为"Test_Content"的原因。 虽然在Cookie Version 1规定中放宽了限制,可以使用这些字符,但是考虑到新版本的Cookie规范目前仍然没有为所有的浏览器所支持,因而为保险起见,我们应该在Cookie的内容中尽量避免使用这些字符。
新闻热点
疑难解答