Page 1/2 of the Javascript advanced tutorial (Continued tutorial 2)

Source: Internet
Author: User

Now you have mastered the advanced string processing and related array concepts. It is time for us to open the Magic cookie bottle. cookie is the information of the person who accesses your website. It actually resides on the user's hard disk. Even if the user has left your site, the cookie still exists on the user's hard disk, if the user returns your site again, the cookie will be sent back to your server together to facilitate statistics and processing of repeated visitors.

Let's take a look at a typical example of a cookie application. We set a cookie in a webpage and then read it from another webpage. when using this example, think about how you can do this without a cookie.

Cookies involve writing disks to users' hard disks and reading information. Therefore, they involve confidentiality. cookie has its own scope and limitations. its most important limitation is that not everyone's browsers welcome cookies. even if the user's browser welcomes cookies, the user may reject the access to cookies (most people are still welcome). Each domain name only allocates 20 cookies, so what are the savings. cookies must not exceed 4 kb. Of course, the capacity of 4,000 bytes is sufficient.

After learning about these limitations, we began to learn how to set cookies. It is easy to set a basic cookie. All you need to do is set cookie_name = Value
Generate a string in the form and set document. cookie property. unique TRICK: the cookie value cannot contain spaces, commas or semicolons. fortunately, you don't need to worry about these issues, because there are a series of functions that can help you encode and decode cookies:

Escape () and Unescape ().

In the following simple example, your name is saved as a cookie:

Function setcookie ()
{
VaR the_name = prompt ("What's your name? ","");

VaR the_cookie = "wm_javascript = Username:" + escape (the_name );

Document. Cookie = the_cookie;

Alert ("thanks, now go to the next page .");
}

The two rows in the middle of the function are the key:
VaR the_cookie = "wm_javascript = Username:" + escape (the_name );

If I enter "Dave thau" in the prompt box, this rowCodeA string wm_javascript = Username: Dave % 20thau will be generated. this means that I will save a cookie named wm_javascript to the hard disk. the cookie value is Username: Dave % 20 thau-function escape (). Replace the space between "Dave" and "thau" with % 20.

When we read the cookie, we look for a cookie named wm_javascript, then extract Username: Dave % 20 Thau, decode it with Unescape (), and remove Username :.

Document. Cookie = the_cookie;

It's easy to set the cookie now.

Once you set a cookie on a user's hard disk, it is easy to read. The following Code reads the cookie sample:

Function readcookie ()
{
VaR the_cookie = Document. Cookie;
VaR broken_cookie = the_cookie.split (":");
VaR the_name = broken_cookie [1];
VaR the_name = Unescape (the_name );

Alert ("Your name is:" + the_name );
}

Row 1st is important. When your browser opens a webpage, it calls any cookies related to the webpage and loads them into the document. Cookie attribute.

The trick to read cookies is to extract the information you need. note that the cookie we set is as follows: wm_javascript = Username: Dave % 20thau. all usernames (usernames) after row 3 of the function are used to extract from the cookie ).

VaR broken_cookie = the_cookie.split (":");
Cut the cookie into two parts by semicolons.

VaR the_name = broken_cookie [1];
Capture the content following the semicolon Dave % 20thau.

VaR the_name = Unescape (the_name );
Cancel encode replacement of function escape (). Replace % 20 with spaces in this example.

Alert ("Your name is:" + the_name );
Show your name.

The cookie used in this example only saves a small amount of information: the user name, the cookie can save up to 4 kb of information. In the next lecture, we will introduce a complex example.

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.