Example of cookie object usage in JavaScript _javascript tips

Source: Internet
Author: User

This example describes the use of cookie objects in JavaScript. Share to everyone for your reference. Specifically as follows:

Property

Name the only property that must be set to represent the cookie
expires specifies the lifetime of the cookie, if not set, the browser turns off automatic expiration
Path determines the availability of cookies to the server for other Web pages, in general, cookies are available to all pages in the same directory, and when the Path property is set, the cookie is only valid for all pages under the specified path and subpath
Domain Many servers are composed of more than one server, domain property is mainly set in the same domain multiple servers share a cookie, if Web server a needs to share cookies with Web Server B, you need to set A's cookie's domain property to B, so A cookies created can be shared by a, b
Secure General SSL-enabled Web sites begin with HTTPS, and the secure attribute sets cookies to be accessible only through HTTPS or other security protocols

Cookies are essentially strings

In general, cookies cannot contain special characters such as semicolons, commas, spaces, and so on, but these characters can be transmitted using encoding, that is, converting private characters in a text string to the corresponding hexadecimal ASCII value, using the encodeURI () function to translate text characters into valid URIs, using the decodeURI () function to decode

Write Cookie

var cookietest = "Name=username"; 
Document.cookie= CookieTest; Deposit 
///semicolon split different attribute 
var date = Newdate (); 
Date.setdate (Date.getdate () +7); Set the cookie's survival time to one week 
document.cookie= encodeURI ("Name=user") + "; expires=" +date.toutcstring (); 

Read cookies

var cookiestring= decodeuri (document.cookie); 
var cookiearray= cookiestring.split (";"); 
for (vari=0;i< cookiearray.length;i++) { 
  var cookienum = cookiearray[i].split ("="); 
  var cookiename = cookienum[0]; 
  var cookievalue = cookienum[1]; 
} 

Delete Cookies

var date = Newdate (); 
Date.settime (Date.gettime () -10000); 
Document.cookie= "name=user;expires=" +date.togmtstring; 
To delete a cookie is to set its expiration time to a time value of the past 

I hope this article will help you with your JavaScript programming.

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.