Jquery. Basic usage of Cookie Plug-ins

Source: Internet
Author: User
Tags jquery library


Basic usage of Jquery.cookie:

Include script after the JQuery library (unless you are packaging scripts somehow else):

<script src= "/path/to/jquery.cookie.js" ></script>

Create Session Cookie:

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


Create expiring cookies, 7 days from then:


$.cookie (' name ', ' value ', {expires:7});

Create expiring cookies, valid across entire site:


$.cookie (' name ', ' value ', {expires:7, path: '} ');
Read Cookie:


$.cookie (' name '); => "Value"
$.cookie (' nothing '); => undefined

Read All available cookies:

$.cookie (); => {"Name": "Value"}

Delete Cookie:


Returns True when Cookie is successfully deleted, otherwise false
$.removecookie (' name '); => true
$.removecookie (' nothing '); => false

Need to use the same attributes (path, domain) as what the cookie is written with
$.cookie (' name ', ' value ', {path: '/'});
This won ' t work!
$.removecookie (' name '); => false
This would work!
$.removecookie (' name ', {path: '/'}); => true

A cookie instance of jquery

jquery Cookie plug-in can facilitate the operation of cookies, first introduced Jquery.cookie.js file, you can go to the official website to download.


<script type= "Text/javascript" src= "__static__/common/js/jquery.cookie.js" ></script>

The following use jquery cookies to implement a simple function, a public bar, you can click to expand or shrink, the first entry to the default expansion of the page, the second after entering the page default contraction, but click can also be expanded.


A cookie from jquery records whether the interface is accessed for the first time:


<script>
(function ($) {
var $trigger = $ ('. Dz_box. Dz_title ');
var $view _cur = $ ('. Dz_title a:first ');
var $view = $ ('. Dz_box. Dz_info ');
var uid = {$businesser _info[' uid ']};

var has_read = $.cookie (' gonggao_uid_ ' + uid);

if (has_read) {
$view. Hide ();
$view _cur.removeclass (' Wel_down01 '). addclass (' wel_up01 ');
}else{
$view _cur.removeclass (' Wel_up01 '). addclass (' Wel_down01 ');
$.cookie (' gonggao_uid_ ' + uid, ' 1 ');
}

$trigger. Click (function () {
if ($view _cur.hasclass (' wel_up01 ')) {
$view. Slidedown ();
$view _cur.removeclass (' Wel_up01 '). addclass (' Wel_down01 ');
} else {
$view. Slideup ();
$view _cur.removeclass (' Wel_down01 '). addclass (' wel_up01 ');
}
});
}) (JQuery);
</script>

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.