Home  >  Article  >  Web Front-end  >  JavaScript Advanced Tutorial (Lesson 2) Page 1/3_Basic Knowledge

JavaScript Advanced Tutorial (Lesson 2) Page 1/3_Basic Knowledge

PHP中文网
PHP中文网Original
2016-05-16 19:15:35763browse

Today we will learn something very useful and interesting: cookies - these are used to record information about people who have visited your web page. Using cookies, you can record the visitor's name and send him a warm welcome message when he visits your site again. You can also use cookies to remember the characteristics of the client - if the visitor is connected to a slow network cable, the cookie can automatically tell you to send only as little image content as possible when sending the web page to them.

As long as you use cookies within a reasonable scope (do not use them to inquire about users’ personal privacy), cookies are still quite useful. So I'm going to introduce you to how cookies work, but before I get started, let's talk about two JavaScript things: interesting string manipulation and related arrays.

Why do you have to learn magical string processing before starting to roam the world of cookies? Because cookies are also strings. To save visitor information, you must first create a special cookie string. This information is then read when the visitor returns to your site, at which point you must decode the cookie string. To generate and interpret these strings you must understand how JavaScript strings work. So we must first understand strings. If you are a newbie, you should first read the content of the second lesson of the JavaScript Basic Tutorial. Here is an example:

var normal_monkey = "I am a monkey!
";
document. writeln("Normal monkey " normal_monkey);
var bold_monkey = normal_monkey.bold();
document.writeln("Bold monkey " bold_monkey);

Statement here:

var bold_monkey = normal_monkey.bold();

is equivalent to the following pair of declarations:

var bold_monkey = "" normal_monkey "";

The first version of the statement looks much more concise. The bold object in the string object is used here. Other string objects include indexOf, charAt, substring, and split. These methods can go deep into the structure of the string. First let's study indexOf.

indexOf
indexOf is used to find the position of a series of characters in a string and 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");
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");
location_of_key (key's position) will be 3 because the substring "key" starts with the letter k, and k is the position of the word monkey It's 3.

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 practical:

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 The code snippet asks the user for their email address. If the email address entered by the user does not contain characters, the user is prompted "@The email address you entered is invalid. The email address must contain the character @."

charAt
The chatAt method is used to find 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 (the first character) is "m"
the_second_letter (the second character) is "o"
the_last_letter (the last character) is "y"

Note that you can find out how many characters it contains by using the length attribute of the string. In this example, the_word is "monkey", so the_word.length is 6. Don't forget that the first character in a string is at position 0, so the last character is at length-1. So the_word.length-1 is used in the last line.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn