Simple use of cookies

Source: Internet
Author: User

A cookie can be used when one page wants to get some value from another page

Create a new cookie.
When hours is an empty string, the lifetime of the cookie is the end of the browser session.
Hours is the number 0 o'clock, which establishes a stale cookie that overwrites a cookie that has been created with the same name and path.
(if this cookie exists).
function Setcookie (name, value, days, path) {
var name = Escape (name);
var value = Escape (value);
var expires = new Date ();
Expires.settime (Expires.gettime () + days * 24 * 60 * 60 * 1000);
Path = Path = = ""? "": ";p ath=" + path;
_expires = (typeof hours) = = "string"? "": "; expires=" + expires.toutcstring ();
Document.cookie = name + "=" + value + _expires + path;
}

Setcookie (' name ', ' Shu ')
Setcookie (' Pass ', ' 123 ')
Get Cookie Value
function GetCookie (name) {
var name = Escape (name);
Read the cookie attribute, which returns all cookies for the document
var allcookies = Document.cookie;

Find the starting position of a cookie named name
Name + = "=";
var pos = allcookies.indexof (name);
If a cookie with that name is found, extract and use its value
if (pos! =-1) {//If the POS value is-1 then the search for "version=" fails
var start = pos + name.length; Where the cookie value begins
var end = Allcookies.indexof (";", start); Search for the first ";" from where the cookie value begins. Location, where the cookie value ends
if (end = =-1) end = Allcookies.length; If the end value is-1 indicates that there is only one cookie in the cookie list
var value = allcookies.substring (start, end); Extract the value of a cookie
return unescape (value); to decode it.
} else return ""; Search failed, return empty string
}

Delete Cookies
function Deletecookie (name, path) {
var name = Escape (name);
var expires = new Date (0);
Path = Path = = ""? "": ";p ath=" + path;
Document.cookie = name + "=" + "; expires=" + expires.toutcstring () + path;
}

Example: Using a cookie in a page to save a value: Setcookie ("Key", "value", 1, "/");

Get the value in page B: GetCookie ("key"), Alert (GetCookie ("key"));

Simple use of cookies

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.