jquery Manipulating cookies (native JavaScript processing cookies)

Source: Internet
Author: User

jquery is also capable of manipulating cookies by 1. First download the jquery.js and jquery.cookie.js two files

2. Install (actually reference)


<!--introduction of jquery-->
<script type= "Text/javascript" src= "Jquery-2.1.1.min.js" ></script>

            <!--introduction of Jquery-->

            <script type= "Text/javascript"
                 $.cookie (' name ', ' 123456789 '); //Set name=123456789 cookie
       
   

3. Common methods 3.1. Create a new cookie

$.cookie (' name ', ' value ');

You can use it directly.

do not need any more

$ (document). Ready (function () {

$.cookie (' name ', ' value ');

});

3.1.1. Setting the cookie expiration Date:

$.cookie (' name ', ' value ', {expires:7});//valid for 7 days

3.1.2. Setting the path and validity period:

$.cookie (' name ', ' value ', {expires:7,path: '/');//valid for 7 days, path is/

3.2. Read the cookie 3.2.1. Read a single cookie

$.cookie (' name ');//= + "value", already assigned cookie

$.cookie (' nothing '); = = Undefined, access to non-existent cookies

3.2.2. Read All Cookies

$.cookie ();//Gets an array {"name": "Value"}

3.3. Delete Cookie 3.3.1. Delete the cookie by name, the success will return true, otherwise false

$.removecookie (' name ');

3.3.2. If you use a cookie with the same name, you need to add a path to delete it correctly

$.cookie (' name ', ' value ', {path: '/'});

$.removecookie (' name ');//Not valid

$.removecookie (' name ', {path: '/'});//This is the right one.

For more information, refer to: Https://github.com/carhartl/jquery-cookie#readme

Native JavaScript can also achieve the above functions, just a little bit of trouble

New Cookie:

document.cookie= "Name=value";

Read cookies:

var cookies = document.cookie;

Interested friends can view: http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html (Thanks to this friend's share)

jquery Manipulating cookies (native JavaScript processing 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.