JavaScript Basic article--cookie

Source: Internet
Author: User
Tags setcookie

Cookies, simple to understand. is to store the data. With cookies, data can be stored for long periods of time without disappearing as the browser closes.

It is important to note that the location of cookies in the same PC, between different browsers , is not the same, even in the same browser, because cookies is stored in the form of a domain name, so the cookies between different websites are also differentiated. of course, a domain name of the number of cookies stored, each cookie can be stored in the size of the content is limited, different browsers have different settings. a cookie statement can only add one cookie, a name value pair, and multiple cookies need to be added multiple times.

use of cookies:

the use of cookies is divided into three parts, adding cookies,obtaining Cookies, and deleting cookies.

1. Add a cookie:Thecookie is temporarily stored by default and expires at the current time, and disappears automatically when you close the browser process, so you need to generate a cookie and set an expiration time.

Syntax: document.cookie=' age=20; Expires=datas ';

This statement adds a cookie to the right of the assignment statement to ensure that it is in the form of a string. When Chinese and special characters are present in the first name value pair, exceptions may be displayed, so The content of the cookie is best stored in an encoded form. All results can be stored and displayed smoothly through encoding and decoding. expires set the expiration time. The following example provides a better understanding of how cookies are added.

var set_time=new Date ();

Set_time.setdate (Set_time.getdate () +d); First get the current date for the day of the month, and then set the expiration time

Document.cookie= ' username= ' +encode (joe\n Hello ) +'; expires= ' +set_time.togmtstring (); Convert the date format to a string format

2. get cookies: All cookies are concatenated in a semicolon + space. When using document.cookie to obtain a cookie under the current Web site , you get a value in the form of a string that contains all of the current web Cookies. To get the name of each cookie , you first need to process each cookie . Take a look at the method of obtaining cookies in the specific application given below .

3. Delete cookies: Set the cookie expiration time to a time in the past to delete this cookie .

A specific application is given below:

HTML section:

<body>

<input type= "text" id= "username"/>
<input type= "button" value= "Login" id= "Login"/>
<input type= "button" value= "delete" id= "del"/>

</body>

JS section::

Window.onload=function () {

var Login=document.getelementbyid ("Login");

var Del=document.getelementbyid ("Del");

var Ousername=document.getelementbyid ("username");

if (GetCookie (' username ')) {

Ousername.value=getcookie (' username ');

}

Login.onclick=function () {

Alert (' login successful ');

Setcookie (' username ', ousername.value,5);

}

Del.onclick=function () {

Removecookie (' username ');

Ousername.value= "; Good user experience, delete cookies, user name blank, editable status

}

}

Add a cookie

function Setcookie (key, value, T) {//name=joe:key is name,Joe is value , T is the expiration time

var cleardate=new Date ();

Cleardate.setdate (Cleardate.getdate () + t);

document.cookie= key + ' = ' + value + '; expires= ' + cleardate.toutcstring (); The time object is converted to a string form

}

Get cookies

function GetCookie (key) {//cookie form:name=joe; age=20; ....

var arr1=document.cookie.split (';      ‘); the cookie string to ; split into arrays, [name=joe],[age=20],...

for (var i=0; i<arr1.length; i++) {// traversal of the above array member

var arr2 = arr1[i].split (' = '); Splits Each member of the above array into arrays again in = interval arr2

if (arr2[0] = = key) {// there are 2 members in arr2 at this time , then the first array element of ARR2 is entered Row matching

Return decodeURI (arr2[1]); When a condition is met, the second member of the arr2 , the value of the key-value pair, is returned as a decoded form ;

}

}

}

Delete Cookies

function Removecookie (key) {

Setcookie (Key, ',-1); time is set to the past time, you can delete

}

JavaScript Basic article--cookie

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.