jquery Gets the cookie value and deletes the cookie usage instance _jquery

Source: Internet
Author: User

This example describes jquery getting cookie values and removing cookie usage. Share to everyone for your reference, specific as follows:

Cookies have the specified cookie action class in jquery, and here are some questions about how to use cookies to manipulate classes, and then describe the correct ways to use them.

Incorrect value occurred while using jquery to manipulate cookies:

The result is that cookies have four different properties:

Name, content, domain, path

$.cookie (' The_cookie '); Read Cookie
$.cookie (' The_cookie ', ' the_value ');//Storage Cookie
$.cookie (' The_cookie ', ' The_value ', {expires:7} ); Store a cookie with a 7-day term
$.cookie (' The_cookie ', ', {Expires:-1});//Delete Cookie

Use:

$.cookie ("Currentmenuid", MenuID);

The domain and path were not specified.

So different cookies are generated when the domain and path are different

$.cookie ("Currentmenuid");

A problem occurs when a value is taken.

Therefore, use:

$.cookie ("Currentmenuid", "MenuID", {path: "/"});

to overwrite. A cookieid corresponds to a value in the same field.

Now let's take a look at an example

The path setting for cookies requires that if you do not set path: '/', path will be automatically set according to the directory (such as: Http://www.xxx.com/user/,path will be set to '/user ')

$.extend ({/** 1) Sets the value of the cookie to set the value of the name variable to value example $.cookie (' name ', ' value ');
2. Create a new cookie including the expiration path domain name example $.cookie (' name ', ' value ', {expires:7, path: '/', Domain: ' jquery.com ', secure:true});
3. New Cookie Example $.cookie (' name ', ' value ');
4. Delete a cookie example $.cookie (' name ', null);
5. Take a cookie (name) value to myvar var account= $.cookie (' name '); **/cookiehelper:function (name, value, options) {if (typeof value!= ' undefined ') {//name and value given, set C Ookie options = Options | |
      {};
        if (value = = null) {value = ';
      Options.expires =-1;
      } var expires = ';
        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 * 24 * 60 * 60 * 1000));
        else {date = Options.expires; } expires = '; Expires= ' + date.toutcstring (); Use expires attribute, Max-age isn't supported by IE} var path = Options.path? ';
      Path= ' + options.path: '; var domain = Options.domain? ';
      Domain= ' + options.domain: '; var secure = options.secure? ';
      Secure ': ';
    Document.cookie = [name, ' = ', encodeURIComponent (value), expires, path, domain, Secure].join (');
      else {//only name given, get cookie var cookievalue = null;
        if (document.cookie && document.cookie!= ') {var cookies = Document.cookie.split (';');
          for (var i = 0; i < cookies.length i++) {var cookie = Jquery.trim (Cookies[i]);
          Does this cookie string begin with the name we want? if (cookie.substring (0, name.length + 1) = = (name + ' = ')) {cookievalue = decodeURIComponent (cookie.substring (
            Name.length + 1));
          Break
    }} return cookievalue;

 }
  }
});

More interested readers of jquery-related content can view this site: "JQuery Cookie Tips Summary", "jquery table (table) Operation Tips Summary", "jquery drag-and-drop effects and tips summary", "jquery extension Techniques Summary", " jquery Common Classic Effects Summary "jquery animation and special effects usage Summary", "jquery selector usage Summary" and "jquery common Plug-ins and Usage summary"

I hope this article will help you with the jquery program design.

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.