Home Web Front-end JS Tutorial Example analysis of cookie object usage in javascript_javascript skills

Example analysis of cookie object usage in javascript_javascript skills

May 16, 2016 pm 04:16 PM
cookie javascript object usage

The examples in this article describe the usage of cookie objects in JavaScript. Share it with everyone for your reference. The details are as follows:

Attributes

name The only attribute that must be set, indicating the name of the cookie
expires Specifies the lifetime of the cookie. If it is not set, it will automatically expire when the browser is closed
path determines the availability of the cookie to the server for other web pages. Generally, the cookie is available to all pages in the same directory. When the path attribute is set, the cookie is only valid for all web pages under the specified path and sub-path
domain Many servers are composed of multiple servers. The domain attribute mainly sets multiple servers in the same domain to share a cookie. If web server a needs to share cookies with web server b, the domain attribute of a's cookie needs to be set to b, so that a The created cookie can be shared by a and b
secure Generally, websites that support SSL start with HTTPS. The secure attribute can set cookies that can only be accessed through HTTPS or other security protocols

Cookies are essentially strings

Generally speaking, cookies cannot contain special characters such as semicolons, commas, and spaces. However, these characters can be transmitted using encoding, that is, the special characters in the text string are converted into corresponding hexadecimal ASCII values. Use the encodeURI() function to convert text characters into a valid URI, and use the decodeURI() function to decode

Write cookie

1

2

3

4

5

6

var cookieTest ="name=userName";

document.cookie= cookieTest; //存入

//用分号分割不同属性

var date = newDate();

date.setDate(date.getDate()+7); //设置cookie的存活时间为一星期

document.cookie= encodeURI("name=user")+";expires="+date.toUTCString();

Copy after login

Read cookie

1

2

3

4

5

6

7

var cookieString= decodeURI(document.cookie);

var cookieArray= cookieString.split(";");

for(vari=0;i< cookieArray.length;i++){

  var cookieNum = cookieArray[i].split("=");

  var cookieName = cookieNum[0];

  var cookieValue = cookieNum[1];

}

Copy after login

Delete cookies

1

2

3

4

var date = newDate();

date.setTime(date.getTime()-10000);

document.cookie= "name=User;expires="+date.toGMTString;

//删除一个cookie就是将其过期时间设置为过去的一个时间值

Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Analyze the usage and classification of JSP comments Analyze the usage and classification of JSP comments Feb 01, 2024 am 08:01 AM

Classification and Usage Analysis of JSP Comments JSP comments are divided into two types: single-line comments: ending with, only a single line of code can be commented. Multi-line comments: starting with /* and ending with */, you can comment multiple lines of code. Single-line comment example Multi-line comment example/**This is a multi-line comment*Can comment on multiple lines of code*/Usage of JSP comments JSP comments can be used to comment JSP code to make it easier to read

Usage of WPSdatedif function Usage of WPSdatedif function Feb 20, 2024 pm 10:27 PM

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

How to correctly use the exit function in C language How to correctly use the exit function in C language Feb 18, 2024 pm 03:40 PM

How to use the exit function in C language requires specific code examples. In C language, we often need to terminate the execution of the program early in the program, or exit the program under certain conditions. C language provides the exit() function to implement this function. This article will introduce the usage of exit() function and provide corresponding code examples. The exit() function is a standard library function in C language and is included in the header file. Its function is to terminate the execution of the program, and can take an integer

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Detailed explanation and usage introduction of MySQL ISNULL function Detailed explanation and usage introduction of MySQL ISNULL function Mar 01, 2024 pm 05:24 PM

The ISNULL() function in MySQL is a function used to determine whether a specified expression or column is NULL. It returns a Boolean value, 1 if the expression is NULL, 0 otherwise. The ISNULL() function can be used in the SELECT statement or for conditional judgment in the WHERE clause. 1. The basic syntax of the ISNULL() function: ISNULL(expression) where expression is the expression to determine whether it is NULL or

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

How to use Apple shortcuts How to use Apple shortcuts Feb 18, 2024 pm 05:22 PM

How to use Apple shortcut commands With the continuous development of technology, mobile phones have become an indispensable part of people's lives. Among many mobile phone brands, Apple mobile phones have always been loved by users for their stable systems and powerful functions. Among them, the Apple shortcut command function makes users’ mobile phone experience more convenient and efficient. Apple Shortcuts is a feature launched by Apple for iOS12 and later versions. It helps users simplify their mobile phone operations by creating and executing custom commands to achieve more efficient work and

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values ​​are passed and object references are passed.

See all articles