JavaScript manipulation Cookies

Source: Internet
Author: User
Tags date format return setcookie domain client
Cookies

Problem:
Makes it possible to follow the previous settings when accessing a page, or to share data between different pages. For example, when users visit the site to set the size of the page font, then you will want to visit the next time you can still use the same settings to browse, without repeating the settings.
Solution:
When the user browses the page and makes the settings, save the settings in a cookie and read the settings in the cookie the next time you visit.
Refer to the following script:

Utility function to retrieve a expiration data in proper format;
function getexpdate (days, hours, minutes)
{
var expdate = new Date ();
if (typeof (days) = "number" && typeof (hours) = = "Number" && typeof (hours) = = "Number")
{
Expdate.setdate (expdate.getdate () + parseint (days));
Expdate.sethours (expdate.gethours () + parseint (hours));
Expdate.setminutes (expdate.getminutes () + parseint (minutes));
return expdate.togmtstring ();
}
}

Utility function called by GetCookie ()
function Getcookieval (offset)
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr = = 1)
{
Endstr = Document.cookie.length;
}
Return unescape (document.cookie.substring (offset, endstr));
}

primary function to retrieve cookies by name
function GetCookie (name)
{
var arg = name + "=";
var alen = Arg.length;
var clen = document.cookie.length;
var i = 0;
while (I < Clen)
{
var j = i + Alen;
if (Document.cookie.substring (i, j) = arg)
{
Return Getcookieval (j);
}
i = Document.cookie.indexOf ("", I) + 1;
if (i = = 0) break;
}
Return
}

Store cookie value with optional details as needed
function Setcookie (name, value, expires, path, domain, secure)
{
Document.cookie = name + "=" + Escape (value) +
((expires)? "; Expires= "+ Expires:" "") +
((path)? "; Path= "+ Path:" "") +
(domain)? "; domain= "+ Domain:" "") +
(secure)? "; Secure ":" ");
}

Remove the cookie by setting ancient expiration date
function Deletecookie (name,path,domain)
{
if (GetCookie (name))
{
Document.cookie = name + "=" +
((path)? "; Path= "+ Path:" "") +
(domain)? "; domain= "+ Domain:" "") +
"; Expires=thu, 01-jan-70 00:00:01 GMT ";
}
}
Use the GetCookie (name) function to read the saved value in the cookie, and the parameter name is the name of the cookie item. Returns an empty string if the cookie entry does not exist.
Use the Setcookie () function to save the value of the cookie item, where the first to second two parameters are the name and value of the cookie item. If you want to set an expiration time for it, you need to set a third parameter, which requires a getexpdate () to get the correct format parameter.
Finally, you use Deletecookie () to delete an existing cookie entry, which is actually to expire the item.
Cookies store the data on the client. The script for the page can only read cookie values for the domain and server, and if there are multiple servers in the domain, you need to set the fifth parameter to specify the server. The capacity of the browser is generally limited to 20 name/value per server, each cookie item does not exceed 4,000 characters, more realistically, a single cookie item should be less than 2000 characters, that is, do not use cookies to save large capacity data on the client.
Different browsers save cookies differently. IE creates a text file for each domain cookie, while Netscape stores all cookies in the same text file.
Note: Cookies are stored on the client, so they are affected by browser settings, such as a user may disable cookies. To detect whether the browser supports cookies, use attribute navigator.cookieenabled to determine them.


Reference: (oreilly) Java Script and Dhtml Cookbook.chm



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.