What is Cookie?
Cookie is a variable stored in a computer. Whenever the same computer requests a page through a browser, this cookie is sent. You can use JavaScript to create and retrieve the value of cookies.
Cookie usage scenario:
Name Cookie
When the visitor visits the page for the first time, he or she may fill in his/their names. The name will be stored in Cookie. When the visitors visit the website again, they will receive a welcome word similar to "Welcome John Doe!". The name is taken back from Cookie.
Cable Cookie
When the visitor visits the page for the first time, he or she may fill in his/their passwords. The password can also be stored in cookies. When they visit the website again, the password will be taken back from cookies.
Date Cookie
When the visitor visits your website for the first time, the current date can be stored in Cookie. When they visit the website again, they receive a message like this: "Your Last Visit Was on Tuesday August 11, 2005!". The date is also taken back from cookies.
Create and store cookies
In this example, we want to create a cookie that stores the name of the visitors. When the visitors visited the website for the first time, they were asked to fill in their names. The name will be stored in Cookie. When the visitors visit the website again, they will receive welcome words.
First of all, we will create a function that can store the interviewer's name in the Cookie variable:
Creating cookie is actually fighting string:
"username = amosli; expires = mon, 16 dec 2013 16:20:04 gmt"
Then document.cookie = the string above.
Copy code code as follows:
Function SetCookie (C_Name, Value, ExpireDays)
{{
var expondate = new date ()
Exdate.setdate (exdate.getdate ()+ExpireDays)
document.cookie = c_name+ "="+ Escape (Value)+
((expireds == null)? ":": ";; expires ="+exdate.togmtring ())
}
The parameters in the above function have the name, value, and the number of days of the cookie.
In the above functions, we first converted the number of days with a valid date, and then we store the cookie name, value and its expiration date into the Document.cookie object.
After that, we want to create another function to check whether the cookie has been set:
The core of getCookie () is document.cookie. The remaining part is the processing of the string.
Copy code code as follows:
Function GetCookie (C_name)
{{
if (document.cookie.Length> 0)
{{
c_start = document.cookie.indexof (c_name + "=")
/*
Cookie is a set of string: "username = amosli; aspSessionidqaAdBDD = ggijnhcdkgnfegjiifdnion; __utma = 119627022.987137205055.1387205.1387205 055.1387208465.2; __utmb = 119627022.3.10.1387208465; __utmc = 119627022; __utmz = 119627022.1387208465.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.utmcsr | UTMCCN = (Organic) | UTMCMD = ORGANIC | UTMCTR = (Not%20Provided) "
*/
if (c_start! =-1) {{
C_Start = C_Start + C_name.length + 1
C_END = DOCUMENT.COOKIE.INDEXOF (";", C_START)
if (C_END ==-1) C_END = DOCUMENT.COOKIE.LENGTH
Return Unescape (document.cookie.substring (C_Start, C_END))
}
}
Return "" ""
}
The above functions will first check whether there is a cookie in the Document.cookie object. If the Document.cookie object has some cookies, it will continue to check whether our specified cookies have been stored. If we find the cookies we want, return the value, otherwise the empty string will be returned.
Finally, we want to create a function. The function of this function is: if the cookie has been set, the welcome word is displayed, otherwise the prompt box is required to ask the user to enter the name.
Copy code code as follows:
Function CheckCookie ()
{{
username = getCookie ('username')
if (username! = NULL && Username! = "")
{Alert ('WELCOME AGAIN+Username+'! ')}
else
{{
username = Prompt ('Please Enter your name:', "")
if (username! = NULL && Username! = "")
{{
SetCookie ('Username', Username, 365)
}
}
}
This is all code:
Copy code code as follows:
<html>
<head>
<script type = "text/javascript">
Function GetCookie (C_name)
{{
if (document.cookie.Length> 0)
{{
c_start = document.cookie.indexof (c_name + "=")
if (C_Start! =-1)
{{
C_Start = C_Start + C_name.length + 1
C_END = DOCUMENT.COOKIE.INDEXOF (";", C_START)
if (C_END ==-1) C_END = DOCUMENT.COOKIE.LENGTH
Return Unescape (document.cookie.substring (C_Start, C_END))
}
}
Return "" ""
}
Function SetCookie (C_Name, Value, ExpireDays)
{{
var expondate = new date ()
Exdate.setdate (exdate.getdate ()+ExpireDays)
document.cookie = c_name+ "="+ Escape (Value)+
((expireds == null)? ":": ";; expires ="+exdate.togmtring ())
}
Function CheckCookie ()
{{
username = getCookie ('username')
if (username! = NULL && Username! = "")
{Alert ('Welcom Again'+Username+'!')}
else
{{
username = Prompt ('Please Enter your name:', "")
if (username! = NULL && Username! = "")
{{
SetCookie ('Username', Username, 365)
}
}
}
</script>
</head>
<body only = "checkcookie ()">
</body>
</html>