Today we will learn a useful and interesting content: Cookies-This is used to record the information of the person who visited your Web page. Using cookies, you can record the visitor's name and send a warm welcome message when the visitor visits your site again. You can also use cookies to memorize the characteristics of the client-if a visitor's network cable is slow, the cookie can automatically tell you to send only as few pictures as possible when sending the page.
Cookies are quite practical as long as you use cookies within a reasonable range (don't use it to inquire about the user's personal privacy). So I'm going to introduce you to how cookies work, but before we get started, we'll talk about two JavaScript content: interesting string processing and related arrays.
Why must you learn the Magic string processing before you start the cookie world roaming? Because cookies are also strings. To save the visitor's information, you must first create a special cookie string. Then read the message when the visitor returns to your site, and at this point you must decode the cookie string. To generate and interpret these strings you must understand how the JavaScript string works. So we have to understand the string first. If you're a novice, you should read the second lesson in the JavaScript beginner tutorial, and here's 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 ();
is equivalent to the following statement:
var bold_monkey = "<b>" + Normal_monkey + "</b>";
The 1th version of the statement looks much simpler. This uses the bold object in the String object, other string objects, indexof, charAt, substring, and split, which can drill down into the composition of the string. First of all, let's look at indexof.
IndexOf
IndexOf is used to discover the position of a series of characters in a string and to tell you the starting position of the substring. If a string does not contain the 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");
The location_of_m (the position of the letter m) will be 0, because the letter M is at the beginning of the string. var location_of_o = The_word.indexof ("O"); Location_of_o (the position of the letter O) will be 1.
var Location_of_key = the_word.indexof ("key");
The Location_of_key (key position) will be 3 because the substring "key" begins with the letter K, and K in the word monkey position is 3.
var location_of_y = the_word.indexof ("y");
location_of_y) is the position of the letter Y (5).
var cheeky = The_word.indexof ("q");
The cheeky value is-1, because there is no letter Q in the word "monkey".
The IndexOf more practical place:
var the_email = prompt ("What's your email address?", "");
var the_at_is_at = The_email.indexof ("@");
if (the_at_is_at = = 1)
{
Alert ("Your loser, email addresses must have @ signs in them.");
}
This code asks the user's e-mail address and prompts the user if the user enters an e-mail address that does not contain characters. The e-mail address you entered is not valid and the e-mail address must contain the character @. "
CharAt
The Chatat method is used to discover characters in a particular 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 (1th character) is "M"
The_second_letter (2nd character) is "O"
The_last_letter (last character) is "Y"
Note that the length (length) property of the string can be used to find out how many characters it contains. In this case, the The_word is "monkey", so the the_word.length is 6. Don't forget that the position of the 1th character in a string is 0, so the position of the last character is length-1. So I used the the_word.length-1 in the last line.
Current 1/3 page
123 Next read the full text