//寫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'));
如下是一個很是實用的 javascript 讀寫 Cookie 函數javascript
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.length end = 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文件裏,全站包含。java
<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>
Cookie的格式有2個不一樣的版本,第一個版本,咱們稱爲Cookie Version 0,是最初由Netscape公司制定的,也被幾乎全部的瀏覽器支持。而較新的版本,Cookie Version 1,則是根據RFC 2109文檔制定的。爲了確保兼容性,JAVA規定,前面所提到的涉及Cookie的操做都是針對舊版本的Cookie進行的。而新版本的Cookie目前還不被Javax.servlet.http.Cookie包所支持。瀏覽器
一樣的Cookie的內容的字符限制針對不一樣的Cookie版本也有不一樣。在Cookie Version 0中,某些特殊的字符,例如:空格,方括號,圓括號,等於號(=),逗號,雙引號,斜槓,問號,@符號,冒號,分號都不能做爲Cookie的內容。這也就是爲何咱們在例子中設定Cookie的內容爲"Test_Content"的緣由。cookie
雖然在Cookie Version 1規定中放寬了限制,可使用這些字符,可是考慮到新版本的Cookie規範目前仍然沒有爲全部的瀏覽器所支持,於是爲保險起見,咱們應該在Cookie的內容中儘可能避免使用這些字符。dom