Sorted out two simple JavaScript manipulation cookies, all of which have three functions: setting cookies, querying cookies, and deleting cookies. The first is easy to understand, and the second one is easy to use. Native JS set cookies.
Set Cookies First
Each cookie is a name/value pair, and you can assign a string such as the following to Document.cookie:
Document.cookie= "userid=125";
If you want to store more than one name/value pair at a time, you can use a semicolon plus a space (; ) separated, for example:
Document.cookie= "userid=828; Username=ufo ";
You cannot use semicolons (;), commas (,), Equals (=), and spaces in the name or value of a cookie. Do it in the name of the cookie
This is easy to do, but the value to be saved is indeterminate. How do you store these values? method is to use the escape () function to edit
Code that can use hexadecimal notation for some special symbols, such as a space that will be encoded as "20%" so that it can be stored in
Cookie value, and the use of this scheme can also avoid the appearance of Chinese garbled. For example:
Document.cookie= "str=" +escape ("I love UFO");
Equivalent:
Document.cookie= "Str=i%20love%20ufo";
When you use Escape () encoding, you need to use unescape () to decode after the value is fetched to get the original cookie value.
This has been introduced before.
Although Document.cookie looks like an attribute, it can be assigned a different value. But it's not the same as the general properties that change
Its assignment does not mean losing the original value, such as executing the following two statements consecutively:
Document.cookie= "userid=125";
Document.cookie= "Username=ufo";
The browser will maintain two cookies, respectively UserID and username, so give Document.cookie a value like a
Lines that are similar to these statements:
Document.addcookie ("userid=125");
Document.addcookie ("Username=ufo");
In fact, the browser is in this way to set cookies, if you want to change the value of a cookie, you just have to reassign
Values, such as:
Document.cookie= "userid=451";
This sets the cookie value named UserID to 451.
Get the value of a cookie
Here are a few cookies operating functions
Simple version:
| code is as follows |
copy code |
function Setcookie (name, value, Iday) { var odate = new Date (); Odate.setdate (odate.getdate () + iday); Document.cookie = name+ ' = ' +value+ '; expires= ' +odate+ ';p ath=/'; } function GetCookie (name) { var arr = Document.cookie.split ('; '); var i = 0; For (i=0 i<arr.length; i++) { var arr2 = arr[i].split (' = '); if (arr2[0] = = name) {return arr2[1];} } Return "; } function Removecookie (name) { Setcookie (Name, ',-1); } |
Package version:
| The code is as follows |
Copy Code |
| var cookie=new function () { This.set=function (name,value,hours) { var life=new Date (). GetTime (); life+=hours*1000*60*60; var cookiestr=name+ "=" +escape (value) + "; expires=" +new Date (Life). toGMTString () + ";p ath=/"; DOCUMENT.COOKIE=COOKIESTR; }; This.get=function (name) { var cookies = Document.cookie.split (";"); var i = 0; For (i=0 i<cookies.length; i++) { var cookie2=cookies[i].split ("="); if (cookie2[0]==name) {return unescape (cookie2[1]);} } Return "; }; This.remove=function (name) { var cookiestr=name+ "=" "+escape (' null ') +"; expires= "+new Date (). togmtstring (); DOCUMENT.COOKIE=COOKIESTR; }; } |