JavaScript Advanced Tutorial (Lesson 2) Page 1/3_Basic Knowledge
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
