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