Javascript encapsulation Cookie application interface _ javascript tips-js tutorial

Source: Internet
Author: User
This article uses several simple examples to show you the precautions and operation methods for javascript cookie encapsulation. It is very simple and practical. A specific example is provided at the end, if you need a small torch, you can refer to it. This article records some of the things I have learned in learning cookies, deepen my memory and record them, so that I can easily review them later.

Encapsulation Function

By default, it is troublesome to access cookies. Because cookies store information through strings, it is easy to convert the data type for reading information during the value assignment operation. In addition, the string of Cookie information is annoying, which is especially inconvenient for Web applications that frequently use Cookie information. Therefore, we need to encapsulate a Cookie function to provide development efficiency!

Defines a function Cookie (). This function can write the specified Cookie information, delete the specified Cookie information, and read the Cookie value of the specified name. In addition, in this function, you can also set the Cookie information's validity period, valid path, scope, and security options. Complete code:

Var Cookie = function (name, value, options) {// if the second parameter has if (typeof value! = 'Undefined') {options = options | |{}; if (value = null) {// set the expiration time options. expires =-1;} var expires = ''; // if there is an event parameter item and its type is number or the specific time, set the event if (options. expires & (typeof options. expires = 'number' | options. expires. toUTCString) {var date; if (typeof options. expires = 'number') {date = new Date (); date. setTime (date. getTime () + (options. expires * 24x60*60*1000);} els E {date = options. expires;} expires = '; expires =' + date. toUTCString ();} var path = options. path? '; Path =' + options. path: '', // set the path domain = options. domain? '; Domain =' + options. domain: '', // set the domain secure = options. secure? '; Secure': ''; // sets the security measure. If it is set to true, It is set directly. Otherwise, it is null. // stores all string information into an array, and then calls join () method, and write the Cookie information document. cookie = [name, '=', encodeURIComponent (value), expires, path, domain, secure]. join ('');} else {// if the second parameter does not exist var CookieValue = null; if (document. cookie & document. cookie! = '') {Var Cookie = document. cookie. split (';'); for (var I = 0; I <Cookies. length; I ++) {var Cookie = (Cookie [I] | ""). replace (/^ \ s + | \ s + $/g, ""); if (Cookie. substring (0, name. length + 1) = (name + '=') {CookieValue = decodeURIComponent (Cookie. substring (name. length + 1); break ;}}return CookieValue ;}};

How to Use

Write Cookie information:

// Write a Cookie ("user", "baidu"); // write a cookie and set more options: Cookie ("user", "baidu ", {expires: 10, // valid for 10 days path: "/", // valid domain for the entire site: "www.baidu.com", // valid domain name secure: true // encrypted data transmission });

2. Read Cookie information:

cookie("user");

3. Delete Cookie information:

cookie("user", null);

I will share with you a encapsulated code.

// Write data to the cookie function writeCookie (name, value, days) {// defines the effective date (cookie's effective time) var expires = ""; // assign a valid date value if (days) {var Date = new date (); // set the validity period (current time + time period) date. setTime (date. getTime () + (days * 24*60*60*1000); // The number of milliseconds expires = "; expires =" + date. toGMTString () ;}// assign name, value, and expiration date (validity period) to the cookie document. cookie = name + "=" + value + expires + "; path =/";} // read cookie data function readCookie (name) {var searchName = name + "= "; var cookies = document. cookie. split (';'); for (var I = 0; I <cookies. length; I ++) {var c = cookies [I]; while (c. charAt (0) = '') c = c. substring (1, c. length); if (c. indexOf (searchName) = 0) return c. substring (searchName. length, c. length);} return null;} // clear all cookiefunction eraseCookie (name) {// set the time to-1 to clear the data writeCookie (name, "",-1 );}

Finally, if there are any errors or questions in the article, please point out. I would like to share with you sf!

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.