0
点赞
收藏
分享

微信扫一扫

JQuery中操作cookie


安装jquery插件

下载地址:​​https://github.com/carhartl/jquery-cookie/releases/download/v1.4.1/jquery.cookie-1.4.1.min.js​​

前提要包含jquery库:

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

也可以直接用百度等其他的公共库:

<script src="http://apps.bdimg.com/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquery.cookie/1.4.1/jquery.cookie.min.js"></script>

使用方法

// Create session cookie:
$.cookie('name', 'value');

// Create expiring cookie, 7 days from then:
$.cookie('name', 'value', { expires: 7 });

// Create expiring cookie, 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 was successfully deleted, otherwise false
$.removeCookie('name'); // => true
$.removeCookie('nothing'); // => false

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

参考:
​​​https://github.com/jquery/jquery​​​
​​​https://github.com/carhartl/jquery-cookie​​​
​​​http://cdn.code.baidu.com/​​


举报

相关推荐

0 条评论