{"id":596,"date":"2011-01-17T21:07:51","date_gmt":"2011-01-17T21:07:51","guid":{"rendered":""},"modified":"2011-01-17T21:07:51","modified_gmt":"2011-01-17T21:07:51","slug":"596","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=596","title":{"rendered":"jquery\u63d2\u4ef6cookie\u7684\u7528\u6cd5\u8be6\u89e3"},"content":{"rendered":"<p>jQuery.cookie.js\u662f\u4e2a\u5f88\u597d\u7684cookie\u63d2\u4ef6\uff0c\u5927\u6982\u7684\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b<br \/><code><br \/>example $.cookie(\u2019name\u2019, \u2018value\u2019);<br \/><\/code><br \/>\u8bbe\u7f6ecookie\u7684\u503c\uff0c\u628aname\u53d8\u91cf\u7684\u503c\u8bbe\u4e3avalue<br \/><code><br \/>example $.cookie(\u2019name\u2019, \u2018value\u2019, {expires: 7, path: \u2018\/\u2019, domain: \u2018jquery.com\u2019, secure: true});<br \/><\/code><br \/>\u65b0\u5efa\u4e00\u4e2acookie \u5305\u62ec\u6709\u6548\u671f \u8def\u5f84 \u57df\u540d\u7b49<br \/><code><br \/>example $.cookie(\u2019name\u2019, \u2018value\u2019);<br \/><\/code><br \/>\u65b0\u5efacookie<br \/><code><br \/>example $.cookie(\u2019name\u2019, null);<br \/><\/code><br \/>\u5220\u9664\u4e00\u4e2acookie<br \/><code><br \/>var account= $.cookie('name');<br \/><\/code><br \/>\u53d6\u4e00\u4e2acookie(name)\u503c\u7ed9myvar<br \/>\u4ee3\u7801\u5982\u4e0b<br \/><code><br \/>jQuery.cookie = function(name, value, options) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (typeof value != 'undefined') { \/\/ name and value given, set cookie<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options = options || {};<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (value === null) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = '';<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.expires = -1;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var expires = '';<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var date;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (typeof options.expires == 'number') {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date = new Date();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date = options.expires;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expires = '; expires=' + date.toUTCString(); \/\/ use expires attribute, max-age is not supported by IE<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var path = options.path ? '; path=' + options.path : '';<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var domain = options.domain ? '; domain=' + options.domain : '';<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var secure = options.secure ? '; secure' : '';<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');<br \/>&nbsp;&nbsp;&nbsp;&nbsp;} else { \/\/ only name given, get cookie<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var cookieValue = null;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (document.cookie && document.cookie != '') {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var cookies = document.cookie.split(';');<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (var i = 0; i &lt; cookies.length; i++) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var cookie = jQuery.trim(cookies[i]);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Does this cookie string begin with the name we want?<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (cookie.substring(0, name.length + 1) == (name + '=')) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cookieValue = decodeURIComponent(cookie.substring(name.length + 1));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return cookieValue;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>};<br \/><\/code><br \/>\u7136\u540e\u770b\u4e86\u4e0bDiscuz!\u4e2d\u5bf9cookie\u7684\u64cd\u4f5c\u65b9\u6cd5<br \/>\u5982\u4e0b,\u53d1\u73b0\u5c11\u4e86\u4e2a\u904d\u5386\u7528;\u5206\u5272\u7684\u6570\u7ec4\u7684\u5904\u7406<br \/><code><br \/>function getcookie(name) {<br \/>var cookie_start = document.cookie.indexOf(name);<br \/>var cookie_end = document.cookie.indexOf(\";\", cookie_start);<br \/>return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end &gt; cookie_start ? cookie_end : document.cookie.length)));<br \/>}<br \/>function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {<br \/>var expires = new Date();<br \/>expires.setTime(expires.getTime() + seconds);<br \/>document.cookie = escape(cookieName) + '=' + escape(cookieValue)<br \/>+ (expires ? '; expires=' + expires.toGMTString() : '')<br \/>+ (path ? '; path=' + path : '\/')<br \/>+ (domain ? '; domain=' + domain : '')<br \/>+ (secure ? '; secure' : '');<br \/>}<br \/><\/code><\/p>\n<p>\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/carhartl\/jquery-cookie\" target=\"_blank\" rel=\"external\">https:\/\/github.com\/carhartl\/jquery-cookie<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery.cookie.js\u662f\u4e2a\u5f88\u597d\u7684cookie\u63d2\u4ef6\uff0c\u5927\u6982\u7684\u4f7f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-596","post","type-post","status-publish","format-standard","hentry","category-Dream"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=596"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/596\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}