Page 1/3 of the Javascript advanced tutorial (Lesson 2)

Source: Internet
Author: User

Today we will learn a very useful and interesting thing: cookies-this is used to record the information of people who have accessed your webpage. You can use cookies to record the name of a visitor and send a warm welcome message to the visitor when the visitor visits your site again. You can also use cookies to remember the features of the client-if the access speed of the visitor's network cable is slow, cookies automatically tell you to send as few images as possible when sending a webpage.

As long as you use cookies within a reasonable range (do not use them to inquire about the user's privacy), cookies are quite practical. So I want to introduce how cookies work, but before getting started, let's talk about two javascript content: Interesting string processing and related arrays.

Why do we have to learn the magic of string processing before we start to roam the cookies world? Because cookies are strings. To save the visitor information, you must first create a special cookie string. Then, when the visitor returns the information to your site, you must decode the cookie string. To generate and interpret these strings, you must understand how JavaScript strings work. Therefore, we must first understand the string. If you are a beginner, you should first read the content of the second lesson of the Javascript preliminary tutorial. The following is an example:

VaR normal_monkey = "I am a monkey! <Br> ";
Document. writeln ("normal monkey" + normal_monkey );
VaR bold_monkey = normal_monkey.bold ();
Document. writeln ("bold monkey" + bold_monkey );

Here's the statement:

VaR bold_monkey = normal_monkey.bold ();

It is equivalent to the following statement:

VaR bold_monkey = "<B>" + normal_monkey + "</B> ";

The declaration of version 1st looks much more concise. Here we use the bold object in the string object. Other string objects include indexof, charat, substring, and split. These methods can be used to penetrate into the string structure. First, let's look at indexof.

Indexof
Indexof is used to locate a series of characters in a string and tell you the starting position of the substring. If a string does not contain this substring, indexof returns "-1." Here is an example:

VaR the_word = "monkey ";
Let's start with the word "monkey.

VaR location_of_m = the_word.indexof ("M ");
Location_of_m (where the letter M is located) will be 0, because the letter M is located at the starting position of the string. VaR location_of_o = the_word.indexof ("O"); location_of_o (where the letter O is located) will be 1.

VaR location_of_key = the_word.indexof ("key ");
Location_of_key (Key location) is 3 because the sub-string "key" starts with the letter K, and K is 3 in the word monkey.

VaR location_of_y = the_word.indexof ("Y ");
Location_of_y) The position of the letter y is 5.

VaR cheeky = the_word.indexof ("Q ");
The cheeky value is-1, because there is no letter q in the word "monkey.

Indexof is more useful:

VaR the_email = prompt ("what's your email address? ","");
VaR the_at_is_at = the_email.indexof ("@");

If (the_at_is_at =-1)
{
Alert ("You loser, email addresses must have @ signs in them .");
}

This sectionCodeAsk the user's email address. If the email address entered by the user does not contain characters, the system prompts the user "@ the email address you entered is invalid. The email address must contain the character @. "

Charat
The chatat method is used to discover characters at a specific position in a string. Here is an example:

VaR the_word = "monkey ";

VaR the_first_letter = the_word.charat (0 );
VaR the_second_letter = the_word.charat (1 );
VaR the_last_letter = the_word.charat (the_word.length-1 );

The_first_letter (1st characters) is "M"
The_second_letter (2nd characters) is "O"
The_last_letter (the last character) is "Y"

Note that you can use the Length attribute of a string to find the number of characters in length. In this example, the_word is "monkey", so the_word.length is 6. Do not forget that the position of the 1st character in a string is 0, so the last character is length-1. So the the_word.length-1 is used in the last line.

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.