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>