Cookie operations in js

Source: Internet
Author: User

Cookie operations in js
A cookie is a short piece of information stored on a computer's hard disk as a key/value pair. The cookie storage capacity is approximately 4 kb, different browser manufacturers have slight differences in cookie size restrictions. The main essence of cookies is "recognition", which is used to do some things; cookies cannot get any other data from your hard disk, send computer viruses or get your email address. A cookie has a validity period. The default validity period of a cookie is from when the cookie is generated to when the browser is disabled. You can also set the cookie validity period to specify the expiration date; users can also disable or manually delete cookies.

// Add cookiefunction addCookie (objName, objValue, objHours) {var str = objName + "=" + escape (objValue ); // cookie content cookieName = cookieValue if (objHours> 0) {// The expiration time is not set for the last time. When the browser is disabled, the cookie automatically disappears var date = new Date (); var MS = objHours * 360*1000; date. setTime (date. getTime () + MS); str + = "; expires =" + date. toGMTString ();} document. cookie = str;} // The format of the cookie stored in js is: name1 = value1; name1 = value1; nam E1 = value1 when obtaining the cookie content, remember to add the trim method to remove spaces on both sides of the content. Otherwise, a problem may occur $ (function () {var email = ""; var arrStr = document. cookie. split (";"); // obtain all cookies for (var I = 0; I <arrStr. length; I ++) {// the cookie value var temp = arrStr [I]. split ("="); // each cookie is split if (temp [0]. trim () = "emailMsg") {// If the cookie name and input parameter are always email = temp [1] ;}/// indicates that the registration is successful, hide the registration box if (email! = "" & Email! = Null) {$ ("# welcome "). removeClass ("dn"); $ ("# login_msg "). addClass ("dn"); $ ("# welcome_msg "). text ("Welcome:" + email );}});

 


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.