Cookie|javascript
Cookies in Java are used very widely, especially in some online audio and video playback of the Web site, typically using server segment script to put the name of the audio and video files in the cookie, and then read in the client segment with JavaScript. This can hide the name of the file to prevent downloading.
Here's an example of using cookies in a JavaScript. We learn together.
Write cookies
var expirestime=new Date ();
Expirestime.settime (Expirestime.gettime () + 3000);//Save 3 seconds
Document.cookie= "cookie_northsnow=" + Escape ("I am the snow of the Northern") + "expires=" + expirestime.togmtstring ();
Read cookies
var strcookie=unescape (Document.cookie);
var strtt= "cookie_northsnow=";
if (Strcookie.indexof (STRTT) >=0)
... {
var strt1=strcookie.substring (Strcookie.indexof (STRTT) + strtt.length)
var strt2=strt1.substring (0,strt1.indexof (";"))
alert (strT2);
}
But what about a cookie array written by the server side?
For example: Response. Cookies ("Cookie_northsnow") ("name") = "I am northsnow!"
The code to read this cookie is as follows:
var strcookie=unescape (Document.cookie);
var strtt= "Cookie_northsnow=name=";
if (Strcookie.indexof (STRTT) >=0)
... {
var strt1=strcookie.substring (Strcookie.indexof (STRTT) + strtt.length)
var strt2=strt1.substring (0,strt1.indexof (";"))
Alert (Strt2.replace (/+/gi, ""));
}
However, for arrays with multiple members, a careful segmentation of the string is required. For example, an array of cookies is created like this:
Response. Cookies ("Cookie_northsnow") ("name") = "snow in the northern"
Response. Cookies ("Cookie_northsnow") ("age") = "The Age"
Response. Cookies ("Cookie_northsnow") ("sex") = "man Big tofu"
You need to get the code to do this:
var strcookie=new String ();
Strcookie=unescape (Document.cookie);
strtt= "cookie_northsnow="
if (Strcookie.indexof (STRTT) >=0)
... {
var strt1=new String ();
var strs=new String ();
var arrstr=new Array ()
Strt1=strcookie.substring (Strcookie.indexof (STRTT) + strtt.length)
Arrstr=strt1.split ("&");
for (Var i=0;i<arrstr.length;i++)
... {
Strs=arrstr[i];
Switch (strs.substring (0,strs.indexof ("="))
... {
Case "name": Alert ("name = = =" + strs.substring (strs.indexof ("="));
Case "Ages": alert ("age = = =" + strs.substring (strs.indexof ("="));
Case "Sex": alert ("gender = =" + strs.substring (strs.indexof ("="));
}
}
}