Home  >  Article  >  Web Front-end  >  javascript cookies setting, reading, deleting example code_javascript skills

javascript cookies setting, reading, deleting example code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:29:491163browse

I just compiled some articles about the operation of javascript cookies, and I found this article is also good. I recommend everyone to refer to it and choose what you need. The shortcomings are mainly the setting of the path. Friends who like it can combine it.

Copy code The code is as follows:

<script> <br>function SetCookie(name, value) //Two parameters, one is the name of the cookie, the other is the value <br>{ <br>var Days = 30; //This cookie will be saved for 30 days <br>var exp = new Date(); //new Date("December 31, 9998"); <br>exp.setTime(exp.getTime() Days*24*60*60*1000); <br>document.cookie = name "=" escape (value ) ";expires=" exp.toGMTString(); <br>} <br>function getCookie(name)//Get cookies function <br>{ <br>var arr = document.cookie.match(new RegExp("( ^| )" name "=([^;]*)(;|$)")); <br>if(arr != null) return unescape(arr[2]); return null; <br>} <br>function delCookie(name)//Delete cookie <br>{ <br>var exp = new Date(); <br>exp.setTime(exp.getTime() - 1); <br>var cval=getCookie( name); <br>if(cval!=null) document.cookie= name "=" cval ";expires=" exp.toGMTString(); <br>} <br><br>SetCookie ("xiaoqi", " 3") <br>alert(getCookie('xiaoqi')); <br></script>



A very practical javascript reading and writing Cookie function
Copy code The code is as follows:

function GetCookieVal(offset)
//After getting the cookie decoded The value of
{
var endstr = documents.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = documents.cookie.length;
return unescape(documents.cookie.substring(offset, endstr));
}
function SetCookie(name, value)
//Set Cookie value
{
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)
// Delete Cookie
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
documents.cookie = name "=" cval "; expires=" exp.toGMTString();
}
function GetCookie(name)
//Get the original value of 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;
}



Copy code The code is as follows:





If you click OK, As long as the cookies are not clear, you will not be prompted again for future visits. If you do not click OK, you will be prompted every time. Place it in a js file, and the entire site contains
Copy code The code is as follows:






1. Cookie compatibility issues
Cookie format has 2 different versions. The first version, which we call Cookie Version 0, was originally developed by Netscape and is also used by almost all Browser support. The newer version, Cookie Version 1, is based on the RFC 2109 document. In order to ensure compatibility, JAVA stipulates that the operations involving cookies mentioned above are performed for older versions of cookies. The new version of Cookie is currently not supported by the Javax.servlet.http.Cookie package.
2. Cookie content
The character limits of the same cookie content are different for different cookie versions. In Cookie Version 0, some special characters, such as spaces, square brackets, parentheses, equal signs (=), commas, double quotes, slashes, question marks, @ symbols, colons, and semicolons cannot be used as cookies. content. This is why we set the cookie content to "Test_Content" in the example.
Although the restrictions in Cookie Version 1 regulations have been relaxed and these characters can be used, considering that the new version of the Cookie specification is still not supported by all browsers, so to be on the safe side, we should Try to avoid using these characters
in your content
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn