Example of using cookies in JavaScript

Source: Internet
Author: User
Tags array
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 ("="));
}
}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.