JavaScript Learning notes-a simple welcome cookie

Source: Internet
Author: User
Tags setcookie time in milliseconds

10<! Doct0000ype html>2 345<meta charset= "Utf-8"/>6<title> Small white study javascript</title>78<body onload= "Checkcookie ()" >9 Ten<script> One         //Create a new cookie (define a method to create a new cookie) A         functionSetcookie (cname,cvalue,eadays) {//set a cookie with a name of CNAME, a value of Cvalue, and a eadays of expiration days -             varD =NewDate ();//Define a Date object -D.settime (D.gettime () + (Eadays * 24 * 60 * 60 * 1000));//set an expiration time, getTime () to get the current time + (eadays days * 24 hours * 60 Minutes *60 seconds *1000 mm) Here is the conversion to milliseconds, because GetTime () gets the time in milliseconds the             varexpires = "expires=" + d.togmtstring ();//convert the acquired time to a string output -Document.cookie = cname + "=" + Cvalue + ";" + expires;//Output Cookie Cookie name: cookie value; cookie expiry time -         }; -  +         //Read Cookies (define a method to read cookies) -         functionGetCookie (CNAME) {//read a cookie named CNAME +             varName = CNAME + "=";//define name as cookie name = A             varCA = Document.cookie.split (';');//The cookie is separated into an array by a semicolon, which is: [Cookie Name: Cookie value, cookie expiry time] at              for(vari = 0; i < ca.length; i++) { -                 varc = Ca[i].trim ();//remove space at the top of the first CA string -                 if(C.indexof (name) = = 0)returnC.substring (Name.length, c.length);//if the first occurrence of name in C is equal to 0, that is, at the time of the first one, the character of C subscript (Name.length, c.length) is returned . -}return"";//otherwise returns an empty -             -         }; in  -         //Automatic detection of cookies (this is the last place to invoke the decision to decide when to invoke the above two methods) to         functionCheckcookie () { +             varuser = GetCookie ("username");//defines the method that calls GetCookie () and assigns a value to it username -             if(User! = "") {//judge if this method is not empty, that is, when the cookie is not empty theAlert ("Welcome again" + user);//pop up a window that welcomes the user *}Else { $user = prompt ("Please enter your name:", "");//Otherwise, a window will pop up prompting the user to enter a name.Panax Notoginseng                 if(User! = "" && User! =NULL) {//judge if GetCookie () This method reads to a value, NOT null and NULL when -Setcookie ("username", user, 30);//call Setcookie () to create a new cookie named username with a value of user and a 30-day expiration date. the                 }; +             }; A         }; the  +  -  $  $         /*Method Explanation - the Split () method is used to split a string into a string array - var str = "How is you doing today?" the  - document.write (Str.split ("") + "<br/>")//Display as: How,are,you,doing,today?Wuyi document.write (Str.split ("") + "<br/>")//Display as: H,o,w,, A,r,e,, Y,o,u,, D,o,i,n,g,, T,o,d,a,y,? the document.write (Str.split ("", 3))//Display as: How,are,you -  Wu The //trim () method deletes the string header and trailing spaces, but preserves the spaces inside the string as separate words from the word -  About The //indexof () method returns the position of the first occurrence of a specified string value in a string $  - the//substring () method is used to extract the character of a string intermediary between two specified subscripts - var str = "Hello world!" -  A document.write (str.substring (3, 7))//Display as: Lo w*/ +  the</script> -  $  the</body> the

JavaScript Learning notes-a simple welcome 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.