Introduction, encapsulation, addition, acquisition, deletion, and html5cookie in html5

Source: Internet
Author: User

Introduction, encapsulation, addition, acquisition, deletion, and html5cookie in html5

Cookie is the data stored on the user's local terminal.
We use cookies to record passwords and time limits when logging on to a website, such as 7 days and 5 days,
This is the session cycle of the cookie, but the cookie is insecure at the same time. We can view the password entered by the user on the webpage and rely on the server environment,
When writing a cookie again, we need to set the storage path, and the obtained value is a string, and it is very small, only 4 kb,
Next we will encapsulate coookie

Set cookie encapsulation:

Function setCookie (name, value, iDay) {// name of the uploaded value, value, expiration time
If (iDay) {// if there is an expiration time, this condition is executed.

Var oDate = new Date (); // obtain the current event stamp

ODate. setDate (oDate. getDate () + iDay); // set the expiration event

Document. cookie = name + "=" + value + "; path =/; expires =" + oDate; // set cookie

} Else {
// If there is an expiration time, execute this condition to set the cookie
Document. cookie = name + "=" + value + "; path =/"; // name, value, and root directory
}

}

Get cookie value

Function getCookie (name ){

Var arr1 = document. cookie. split (";"); get the cookie value and use ";" to cut it into an array.

For (var I = 0; I <arr1.length; I ++) {// loop this array

Var arr2 = arr1 [I]. split ("="); // you can use the split () method to split a two-dimensional array.

If (arr2 [0] = name) {// returns a two-dimensional array of loops. When the first value is the value you pass, the second value of this array is returned.

Return arr2 [1];

}

}

Return "";

}

Delete cookie

Function removeCookie (name ){

SetCookie ("jack", "1234",-1); // call setCookie again. The key is to set the expiration time to a negative value.

}

The above is my understanding of cookies.

  

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.