How JavaScript performs cookies (1)

Source: Internet
Author: User

This article briefly introduces how to use JavaScript to operate cookies. As early as the advent of HTML, there is no way to record and identify different users between each independent page. Later, people invented the Cookie technology. When a user accesses a webpage, it can create a file on the visitor's machine. We call it a Cookie and write a piece of content into it, to identify different users.

Use JavaScript to operate cookies

I. Preface

Cookie should be a technology that has been used for a long time. As early as the advent of HTML, there was no way to record and identify different users between each independent page. Later, people invented the Cookie technology. When a user accesses a webpage, it can create a file on the visitor's machine. We call it a Cookie and write a piece of content into it, to identify different users. If the next time the user visits the webpage, the user can read the content in the file again, so that the webpage knows that the user has visited the webpage.

Although the current web page production technology has developed much more than a few years ago. However, sometimes cookies can help us a lot. Next, let's take a look at how to use JavaScript to operate cookies when writing JavaScript files.

Ii. Write Cookie

In fact, using JavaScript to operate cookies is very simple. Let's look at the following JavaScript program:

 
 
  1. <Html>
  2. <Head> ...... Omitted in the middle)
  3. </Head>
  4. <Body>
  5. <%
  6. StringcookieName = "Sender ";
  7. Cookiecookie = newCookiecookieName, "Test_Content ");
  8. Cookie. setMaxAge10 );
  9. Response. addCookiecookie );
  10. %>
  11. ...... Other content)
  12. </Body>
  13. </Html>
  14.  

In this way, we set a Cookie. Is it easy?

Let's take a closer look at this Code:

 
 
  1. Cookiecookie=newCookiecookieName,"Test_Content");  
  2.  

This line creates a Cookie object with two initialization parameters. The first parameter cookieName defines the Cookie name, and the last parameter is also a string that defines the Cookie content. That is, the file content we want the webpage to identify on the user's machine.

Next line: cookie. setMaxAge10). The setMaxAge method in the Cookie is called to set the Cookie storage duration to 10 seconds on the hard disk of the user's machine. A Cookie does not exist on the user's hard disk for an indefinite period of time. When a Cookie object is created, we must specify the Cookie retention period. After this period is exceeded, cookie files will no longer work and will be deleted by your browser. If we hope that the Cookie file will still be valid and can be read from the webpage when users access this page next time, we can set the Cookie retention period to a little longer. For example, cookie. setMaxAge365 * 24*60*60) can make the Cookie file valid within one year.


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.