JavaScript Create Cookie

Source: Internet
Author: User
Tags setcookie

Create a welcome cookie.

With the w3s document learning, the JavaScript Cookie Welcome page is finally expanded. Share your own cookie learning experience here. , I hope to be helpful to everyone. I'm not here to explain all the official explanations for Cookie,cookie. Introduce me directly to the demo.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/74/A6/wKiom1Yk4mnxxII1AAGruUoeldk291.jpg "title=" screen shot 2015-10-19 pm 8.11.53.png "alt=" Wkiom1yk4mnxxii1aagruuoeldk291.jpg "/>

first We need to invoke the Welcome page directly when the page is loaded. So you can use the OnLoad method, the OnLoad method, you can load the page each time, the first call. OnLoad actively invokes JavaScript Checkcookie ().

Here is Checkcookier demo

function Checkcookie () { //cookname is the object name of the cookie, Cookvalue is the value expiretime the lifetime integer

  1. var User=getcookie ("username");

  2. if (user!=null && user!= "")

  3. {

  4. Alert (' Hello! ' +user+ ' hope you often visit this website to see Yo! ‘);

  5. }

  6. else{

  7. var name=prompt ("Please Enter Name:", ""); //Use this method to save the content entered in the prompt box into the variable name.

  8. if (name!=null && name!= "")//judgment name is not empty, there is a value in case

  9. {/* Enter the following call Setcookie () method This method requires 3 parameters (cookie name cookie value cookie valid time)

  10. Where the cookie name must be a string, it must be enclosed in double quotes.

  11. */

  12. //username is the object name of the cookie, name is the value of the cookie, and 3 is the valid integer

  13. Setcookie ("username", name,3);

  14. }

  15. }

  16. }






The logic of the 2nd step CHeckcookie () script is to get the cookie object from the Web page GetCookie () First, if there is, the concatenation of the string display, if not the creation of objects with Setcookie () save it. The stored cookie object can be obtained from the cookie the next time you log in. The cookie is saved in the form: " cookie name = cookie value; The time of the cookie is valid" the content in double quotation marks is the key content, including the inside (;) semicolon. The cookie name that is obtained from the cookie and stored in the cookie must be the same. So the username name of the two positions is the same. Where the cookie must have 3 parameter cookie object name, Cookvalue is the value of the cookie's lifetime integer Setcookie method to pass 3 parameters to the following is the first time to enter the first time to save a cookie such as:


  1. function Setcookie (cookname,cookvalue,expiretime)//cookname is the object name of the cookie, Cookvalue is the value expiretime the valid integer

  2. {

  3. //Get a computer up-to-date

  4. var expireminute=new Date ();

  5. //Put valid integers through setdate () and. Getminutes method to obtain the last expiration time

  6. Expireminute.setdate (Expireminute.getminutes () +expiretime);

  7. //In the following format, the object name of the cookie, the value of the Cookvalue cookie, is stored.

  8. document.cookie=cookname+ "=" +cookvalue+ ((expireminute==null)? ":"; expire "+expireminute.togmtstring ());

  9. }




The 3rd step of the cookie parameter requires that the valid time be calculated as the cookie expiration date format. Finally, it is saved by Document.cookie method. The next time you log in, use the cookie name to get the cookie content directly. Such as:

The storage content of the cookie object is: Username=cookievalue;expire time;

  1. function GetCookie (cookname) //can have cookie name to find the content of the corresponding cookie name.

  2. {

  3. if (document.cookie.length>0)

  4. {

  5. //Through. IndexOf method: Find the cookie corresponding to the cookie name,

  6. var stratindex=document.cookie.indexof (cookname);

  7. if (cookname!=-1)

  8. {

  9. stratindex=stratindex+cookname.length+1; //Calculate the location of the cookie name

  10. var endindex=document.cookie.indexof (";", Stratindex); //The cookie value is calculated

  11. if (endindex==-1)

  12. {

  13. Endindex=document.cookie.length;

  14. }

  15. Return document.cookie.substring (Stratindex,endindex); //Return the value of the cookie to Checkcookie ();

  16. }

  17. }

  18. Return "";

  19. }


The method needs to determine whether the cookie exists and whether it expires. Then there is the method of string that intercepts the value contents of the cookie. and return. The next login is the Welcome page content.
















This article is from the Web front end iOS blog, so be sure to keep this source http://mengle.blog.51cto.com/10803421/1704304

JavaScript Create Cookie

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.