JavaScript FAQ (21)--cookie__java

Source: Internet
Author: User
Tags setcookie

18. Cookies

1. Set a cookie (Setting a cookie)

Q: How do I set a cookie with JavaScript?

A: to set a cookie to expire after n days, you can use the following function:

function Setcookie (cookiename,cookievalue,ndays) {
 var today = new Date ();
 var expire = new Date ();
 if (Ndays==null | | ndays==0) Ndays=1;
 Expire.settime (Today.gettime () + 3600000*24*ndays);
 Document.cookie = cookiename+ "=" +escape (cookievalue)
                 + "; expires=" +expire.togmtstring ();
}

Cookie Name:
Cookie Value:
Must expire in: Days from today

2. Read cookies (Reading a cookie)

Q: If the cookie is read by a given name.

A: to read cookies by setting the CookieName by the Setcookie function, you can use the following function:

function Readcookie (cookiename) {
 var thecookie= "" +document.cookie;
 var ind=thecookie.indexof (cookiename);
 if (Ind==-1 | | cookiename== "") return ""; 
 var ind1=thecookie.indexof ('; ', Ind);
 if (ind1==-1) ind1=thecookie.length; 
 Return unescape (thecookie.substring (IND+COOKIENAME.LENGTH+1,IND1));
}


Cookie Name:

3. Delete cookie (deleting a cookie)

Q: How to delete a cookie for a given name.

A: to delete a cookie by a given name, you can set a cookie with the same name, but the expiration date is in the past.

4. Is the cookie available? (Are cookies enabled?)

Q: How do I test whether cookies are available on the user's browser?

A: to test whether the cookie is available, you can set a non-empty cookie and then get it. If you get a cookie value that you set up just now, the cookie is available. If the value gets an empty string, then the cookie is disabled.

This page contains simple code that tests whether to support cookies using Setcookie and Readcookie. The result:

5, the location of the cookie file (cookie files Location)

Q: The cookie file is saved somewhere on the hard drive.

A:The actual location of the cookie file depends on the user's browser and operating system. For example, Netscape Navigator 4.x under Windows saves all cookies as a single file, typically in the following locations:

C:/Program Files/netscape/users/your_name/cookies.txt
Internet Explorer 4.x saves each cookie as a separate file. The name of the cookie file is: Anyuser@WebSiteName.txt. A typical storage path for a cookie file is:
C:/windows/cookies/anyuser@websitename.txt

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.