What is cookie? Manually create and store cookies in js

Source: Internet
Author: User

What is cookie?

Cookie is a variable stored on the visitor's computer. This cookie is sent every time a computer requests a page through a browser. You can use JavaScript to create and retrieve cookie values.
Examples of cookies:

Name cookie
When a visitor visits the page for the first time, he or she may fill in his/her name. The name is stored in the cookie. When a visitor visits the website again, they will receive a message similar to "Welcome John Doe! . The name is retrieved from the cookie.
Password cookie
When a visitor visits the page for the first time, he or she may fill in his/her password. Passwords can also be stored in cookies. When they access the website again, the password will be retrieved from the cookie.
Date cookie
When a visitor visits your website for the first time, the current date can be stored in cookies. When they access the website again, they will receive a message like this: "Your last visit was on Tuesday August 11,200 5! ". The date is also retrieved from the cookie.

Create and store cookies

In this example, we will create a cookie that stores the visitor's name. When visitors access the website for the first time, they are asked to enter their names. The name is stored in the cookie. When a visitor visits the website again, they will receive a welcome word.

First, we will create a function that stores the visitor's name in the cookie variable:
Copy codeThe Code is as follows:
<Span style = "font-size: 14px;"> function setCookie (c_name, value, expiredays)
{
Var exdate = new Date ()
Exdate. setDate (exdate. getDate () + expiredays)
Document. cookie = c_name + "=" + escape (value) +
(Expiredays = null )? "": "; Expires =" + exdate. toGMTString ())
} </Span>

The parameters in the above function contain the cookie name, value, and Expiration days.

In the above function, we first convert the number of days to a valid date, and then store the cookie name, value, and expiration date to the document. cookie object.

Then, we will create another function to check whether the cookie has been set:
Copy codeThe Code is as follows:
<Span style = "font-size: 14px;"> 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 ""
} </Span>

The above function first checks whether the document. cookie object contains a cookie. If the document. cookie object contains certain cookies, it will continue to check whether the specified cookie is saved. If the cookie is found, the return value is returned. Otherwise, an empty string is returned.

Finally, we need to create a function. The function is used to display the welcome word if the cookie has been set. Otherwise, a prompt box is displayed asking you to enter a name.
Copy codeThe Code is as follows:
<Span style = "font-size: 14px;"> 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)
}
}
} </Span>

This is all the code:
Copy codeThe Code is as follows:
<Span style = "font-size: 14px;"> <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 exdate = new Date ()
Exdate. setDate (exdate. getDate () + expiredays)
Document. cookie = c_name + "=" + escape (value) +
(Expiredays = null )? "": "; Expires =" + exdate. toGMTString ())
}

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)
}
}
}
</Script>
</Head>

<Body onLoad = "checkCookie ()">
</Body>
</Html> </span>

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.