Cookie method function set for javascript operations

Source: Internet
Author: User
Javascript operation cookie method function set problems:
This allows you to use the previous settings when accessing the page or share data between different pages. For example, if you set the page font size when visiting a website, you will want to use the same settings for browsing next time without repeated settings.
Solution:
When you browse the page and make settings, save these settings in the cookie and read the settings in the cookie for the next visit.
Refer to the following script:

// utility function to retrieve an expiration data in proper format; function getExpDate(days, hours, minutes) { var expDate = new Date(); if(typeof(days) == "number" && typeof(hours) == "number" && typeof(hours) == "number") { expDate.setDate(expDate.getDate() + parseInt(days)); expDate.setHours(expDate.getHours() + parseInt(hours)); expDate.setMinutes(expDate.getMinutes() + parseInt(minutes)); return expDate.toGMTString(); } } //utility function called by getCookie() function getCookieVal(offset) { var endstr = document.cookie.indexOf(";", offset); if(endstr == -1) { endstr = document.cookie.length; } return unescape(document.cookie.substring(offset, endstr)); } // primary function to retrieve cookie by name function getCookie(name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while(i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { return getCookieVal(j); } i = document.cookie.indexOf(" ", i) + 1; if(i == 0) break; } return; } // store cookie value with optional details as needed function setCookie(name, value, expires, path, domain, secure) { document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); } // remove the cookie by setting ancient expiration date function deleteCookie(name,path,domain) { if(getCookie(name)) { document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"; } }

Use the getCookie (name) function to read the value saved in the cookie. The parameter name is the name of the cookie item. If the cookie does not exist, an empty string is returned.
Use the setCookie () function to save the value of the cookie item. The first and second parameters are the name and value of the cookie item respectively. If you want to set an expiration time for it, you need to set the third parameter. Here, you need to get a correct format parameter through getExpDate.
Finally, you can use deleteCookie () to delete an existing cookie by actually making it expire.
Cookie stores data on the client. The page script can only read the cookie values of the domain and server. If there are multiple servers in the domain, you need to set the fifth parameter to specify the server. The browser capacity is generally limited to 20 name/value pairs per server. Each cookie item cannot exceed 4000 characters. More realistically, a single cookie item should be less than 2000 characters, in other words, do not use cookies to store large volumes of data on the client.
Different browsers use different methods to save cookies. IE creates a text file for the cookies in each domain, while Netscape stores all cookies in the same text file.
Note: The cookie is stored on the client, so it is affected by browser settings. For example, the user may disable the cookie. To check whether the browser supports cookies, use the property navigator. cookieEnabled to determine whether the browser supports cookies.

Script: Author of the cookie writing function: Zhen zhenkai function SetCookie (name, value) // two parameters: one is the name of the cookie and the other is the value {var Days = 30; // This cookie will be saved for 30 days var exp = new Date (); // new Date ("December 31,999 8"); exp. setTime (exp. getTime () + Days * 24x60*60*1000); document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();} function getCookie (name) // The cookie function {var arr = document. cookie. match (new RegExp ("(^ |)" + nam E + "= ([^;] *) (; | $)"); if (arr! = Null) return unescape (arr [2]); return null;} function delCookie (name) // Delete cookie {var exp = new Date (); exp. setTime (exp. getTime ()-1); var cval = getCookie (name); if (cval! = Null) document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();} SetCookie ("xiaoqi", "3") alert (getCookie ('xiaoqi'); script

A very practical javascript function for reading and writing cookies

Function GetCookieVal (offset) // obtain the decoded Cookie value {var endstr = documents. cookie. indexOf (";", offset); if (endstr =-1) endstr = documents. cookie. length; return unescape (events. cookie. substring (offset, endstr);} function SetCookie (name, value) // set the Cookie value {var expdate = new Date (); var argv = SetCookie. arguments; var argc = SetCookie. arguments. length; var expires = (argc> 2 )? Argv [2]: null; var path = (argc> 3 )? Argv [3]: null; var domain = (argc> 4 )? Argv [4]: null; var secure = (argc> 5 )? Argv [5]: false; if (expires! = Null) expdate. setTime (expdate. getTime () + (expires * 1000); events. cookie = name + "=" + escape (value) + (expires = null )? "": ("; Expires =" + expdate. toGMTString () + (path = null )? "": ("; Path =" + path) + (domain = null )? "": ("; Domain =" + domain) + (secure = true )? "; Secure": "") ;}function DelCookie (name) // Delete Cookie {var exp = new Date (); exp. setTime (exp. getTime ()-1); var cval = GetCookie (name); events. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();} function GetCookie (name) // obtain the original Cookie value {var arg = name + "="; var alen = arg. length; var clen = documents. cookie. length; var I = 0; while (I <clen) {var j = I + alen; if (documents. cookie. substring (I, j) = arg) return GetCookieVal (j); I = documents. cookie. indexOf ("", I) + 1; if (I = 0) break;} return null ;}

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.