search
HomeWeb Front-endJS TutorialJavaScript 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.


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
Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor