A cookie is a file that is stored on a user's hard disk, which usually corresponds to a domain name, which is available when the browser accesses the domain name again. As a result, cookies can span multiple pages under one domain name, but cannot be used across multiple domain names.
Different browsers implement cookies differently, but their properties are the same. For example, in Windows 2000 and Windows XP, cookie files are stored in the Documents and Settingsusernamecookie folder. The usual naming format is: UserName@domain.txt.
The cookie mechanism stores information on the user's hard disk, so it can be used as a global variable
| The code is as follows |
Copy Code |
/*
* Set up and get cookies
*/
var Cookie = {}
Write Cookie,key as key, value is
Duration Expiration time (days in units, default 1 days)
Cookie.write = function (Key, value, duration)
{
Cookie.remove (key);
var d = new Date ();
if (Duration <= 0)
Duration = 1;
D.settime (D.gettime () + 1000 * * * duration);
Document.cookie = key + "=" + encodeURI (value) + "; Expires= "+ d.togmtstring () +";p ath=/";
};
Remove Cookie,key as key
Cookie.remove = function (key)
{
var d = new Date ();
if (Cookie.read (key)!= "")
{
D.settime (D.gettime ()-(86400 * 1000 * 1));
Document.cookie = key + "=;expires=" + d.togmtstring ();
}
};
Read Cookie,key is a key
There is no return empty string ""
Cookie.read = function (key)
{
var arr = Document.cookie.match (New RegExp ("(^|)" + key + "= ([^;] *)(;|$)"));
if (arr!= null)
Return decodeURIComponent (arr[2]);
Return "";
};
|
Instance
To use 2 JS files, history.js, key chat records to save and read code. Json.js, the JSON is processed.
| The code is as follows |
Copy Code |
|
var addhistory=function (Num,id) {
Stringcookie=getcookie (' history ');
var stringhistory= ""!=stringcookie?stringcookie: "{history:[]}";
var json=new json (stringhistory);
var e= "{num:" +num+ ", ID:" +id+ "}";
json[' History '].push (e);//Add a new record
Setcookie (' History ', json.tostring (), 30);
}
Show History
var displayhistory=function () {
var P_ele=document.getelementbyid (' history ');
while (P_ele.firstchild) {
P_ele.removechild (P_ele.firstchild);
}
var Historyjson=getcookie (' history ');
var json=new json (Historyjson);
var displaynum=6;
For (i=json[' history '].length-1;i>0;i--) {
Addli (json[' history '][i][' num '],json[' history '][i][' ID '), "history");
displaynum--;
if (displaynum==0) {break;}
}
}
Add an LI element
var addli=function (num,id,pid) {
var a=document.createelement (' a ');
var href= ' product.action?pid= ' +id;
A.setattribute (' href ', href);
var t=document.createtextnode (num);
A.appendchild (t);
var li=document.createelement (' Li ');
Li.appendchild (a);
document.getElementById (PID). appendchild (LI);
}
Add cookies
var setcookie=function (c_name,value,expiredays)
{
var exdate=new Date ()
Exdate.setdate (Exdate.getdate () +expiredays)
cookieval=c_name+ "=" +escape (value) + ((expiredays==null)? "": "; expires=" +exdate.togmtstring ());
alert (cookieval);
Document.cookie=cookieval;
}
Get cookies
function GetCookie (c_name)
{
if (document.cookie.length>0)
{
C_start=document.cookie.indexof (c_name + "=")
if (c_start!=-1)
{
C_start=c_start + c_name.length+1
C_end=document.cookie.indexof (";", C_start)
if (c_end==-1) c_end=document.cookie.length
document.write (document.cookie.substring (c_start,c_end) + "<br>");
Return unescape (document.cookie.substring (c_start,c_end))
}
}
Return ""
} |
The JSON data is as follows
| The code is as follows |
Copy Code |
var JSON = function (Sjson) {
This.objtype = (typeof Sjson);
This.self = [];
(function (S,o) {for (var i in O) {o.hasownproperty (i) && (S[i]=o[i],s.self[i]=o[i])}) (This, (this.objtype== ' string ')? Eval (' 0, ' +sjson): Sjson);
}
Json.prototype = {
Tostring:function () {
return this.getstring ();
},
Valueof:function () {
return this.getstring ();
},
Getstring:function () {
var sA = [];
(function (o) {
var oo = null;
Sa.push (' {');
for (var i in O) {
if (O.hasownproperty (i) && i!= ' prototype ') {
OO = o[i];
if (oo instanceof Array) {
Sa.push (i+ ': [');
for (var b in Oo) {
if (Oo.hasownproperty (b) && b!= ' prototype ') {
Sa.push (oo[b]+ ', ');
if (typeof oo[b]== ' object ') Arguments.callee (oo[b));
}
}
Sa.push ('], ');
Continue
}else{
Sa.push (i+ ': ' +oo+ ', ');
}
if (typeof oo== ' object ') Arguments.callee (OO);
}
}
Sa.push ('}, ');
}) (this.self);
Return Sa.slice (0). Join ('). Replace (/[object object],/ig, '). Replace (/,}/g, '} '). Replace (/,]/g, '] "). Slice (0,-1);
},
Push:function (Sname,svalue) {
This.self[sname] = svalue;
This[sname] = svalue;
}
} |
HTML code
| code is as follows |
copy code |
|
<script Type= "Text/javascript" src= ". /js/json.js "></script>
<script type=" Text/javascript "src=". /js/history.js "></script>
<ul id=" History "
</ul>
<script>
AddHistory ( 15810782304,2);
AddHistory (64654665,2);
AddHistory (6843212,2);
AddHistory (84984432521,2);
SetInterval ("Displayhistory ()", 1000);
</script> |