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
var User=getcookie ("username");
if (user!=null && user!= "")
{
Alert (' Hello! ' +user+ ' hope you often visit this website to see Yo! ‘);
}
else{
var name=prompt ("Please Enter Name:", ""); //Use this method to save the content entered in the prompt box into the variable name.
if (name!=null && name!= "")//judgment name is not empty, there is a value in case
{/* Enter the following call Setcookie () method This method requires 3 parameters (cookie name cookie value cookie valid time)
Where the cookie name must be a string, it must be enclosed in double quotes.
*/
//username is the object name of the cookie, name is the value of the cookie, and 3 is the valid integer
Setcookie ("username", name,3);
}
}
}
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:
function Setcookie (cookname,cookvalue,expiretime)//cookname is the object name of the cookie, Cookvalue is the value expiretime the valid integer
{
//Get a computer up-to-date
var expireminute=new Date ();
//Put valid integers through setdate () and. Getminutes method to obtain the last expiration time
Expireminute.setdate (Expireminute.getminutes () +expiretime);
//In the following format, the object name of the cookie, the value of the Cookvalue cookie, is stored.
document.cookie=cookname+ "=" +cookvalue+ ((expireminute==null)? ":"; expire "+expireminute.togmtstring ());
}
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;
function GetCookie (cookname) //can have cookie name to find the content of the corresponding cookie name.
{
if (document.cookie.length>0)
{
//Through. IndexOf method: Find the cookie corresponding to the cookie name,
var stratindex=document.cookie.indexof (cookname);
if (cookname!=-1)
{
stratindex=stratindex+cookname.length+1; //Calculate the location of the cookie name
var endindex=document.cookie.indexof (";", Stratindex); //The cookie value is calculated
if (endindex==-1)
{
Endindex=document.cookie.length;
}
Return document.cookie.substring (Stratindex,endindex); //Return the value of the cookie to Checkcookie ();
}
}
Return "";
}
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