{"id":1806,"date":"2012-09-01T16:26:02","date_gmt":"2012-09-01T08:26:02","guid":{"rendered":"http:\/\/www.hiheng.com\/blog\/?p=1806"},"modified":"2015-03-11T15:37:06","modified_gmt":"2015-03-11T07:37:06","slug":"%e4%bd%bf%e7%94%a8jquery%e6%8f%92%e6%9d%a5%e6%93%8d%e4%bd%9ccookie-jquery-cookie-js","status":"publish","type":"post","link":"http:\/\/www.hiheng.com\/blog\/?p=1806","title":{"rendered":"\u4f7f\u7528jquery\u63d2\u6765\u64cd\u4f5ccookie \u2014\u2014 JQuery.cookie.js"},"content":{"rendered":"<h4>JQuery.cookie.js \u5e38\u7528\u65b9\u6cd5\uff1a<\/h4>\n<p><strong>1\u3001\u65b0\u5efa\u5e76\u8bbe\u7f6ecookie\u7684\u503c<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u65b0\u5efa\u5e76\u8bbe\u7f6ecookie\u7684\u503c *\/\r\n$.cookie(&quot;hiheng&quot;;, &quot;www.hiheng.com&quot;);\r\n\/\/\r\n<\/pre>\n<p><strong>2\u3001\u8bfb\u53d6Cookie\u503c<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u8bfb\u53d6Cookie\u503c, \u8f93\u51fa&quot;www.hiheng.com&quot; *\/\r\n$.cookie(&quot;hiheng&quot;); \r\n\/\/\r\n<\/pre>\n<p><strong>3\u3001\u8bbe\u7f6ecookie\u7684\u6709\u6548\u671f\uff0cexpires\u5c5e\u6027<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u8bbe\u7f6ecookie\u7684\u6709\u6548\u671f\u4e00\u5468\u3002 *\/\r\n$.cookie(&quot;hiheng&quot;, &quot;www.hiheng.com&quot;,{expires: 7}); \r\n\/\/\r\n<\/pre>\n<p><strong>4\u3001\u8bbe\u7f6ecookie\u7684\u57df\u540d\u8303\u56f4\uff0cdomain\u5c5e\u6027<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u8bbe\u7f6ecookie\u7684\u57df\u540d\u8303\u56f4\uff0chiheng.com *\/\r\n$.cookie(&quot;hiheng&quot;, &quot;www.hiheng.com&quot;, {domain: &quot;hiheng.com&quot;});\r\n\/\/\r\n<\/pre>\n<p><strong>5\u3001\u8bbe\u7f6ecookie\u7684\u6709\u6548\u8def\u5f84\uff0cpath\u5c5e\u6027<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u65b0\u5efa\u4e00\u4e2acookie \u5305\u62ec\u6709\u6548\u671f \u8def\u5f84 \u57df\u540d\u7b49 *\/\r\n$.cookie(&quot;hiheng&quot;, &quot;www.hiheng.com&quot;, {path: &quot;\/&quot;});\r\n\/\/\r\n<\/pre>\n<p><strong>6\u3001\u7efc\u5408\u8bbe\u7f6ecookie\u7684\u5404\u79cd\u5c5e\u6027<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u65b0\u5efa\u4e00\u4e2acookie \u5305\u62ec\u6709\u6548\u671f \u8def\u5f84 \u57df\u540d\u7b49 *\/\r\n$.cookie(&quot;hiheng&quot;, &quot;www.hiheng.com&quot;, {expires: 7, path: &quot;\/&quot;, domain: &quot;hiheng.com&quot;, secure: true});\r\n\/\/\r\n<\/pre>\n<p><strong>7\u3001\u5220\u9664\u4e00\u4e2acookie <\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u5220\u9664\u4e00\u4e2aname\u4e3a\u201dhiheng\u201c\u7684cookie *\/\r\n$.cookie(&quot;hiheng&quot;, null); \r\n\/\/\r\n<\/pre>\n<h4>jquery.cookie.js\u6e90\u7801\uff0c<a href=\"http:\/\/www.hiheng.com\/jquery\/jquery.cookie.js\" target=\"_blank\">\u4e0b\u8f7d<\/a><\/h4>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\njQuery.cookie = function(name, value, options) {\r\n    if (typeof value != &#039;undefined&#039;) { \r\n        options = options || {};    \r\n        if (value === null) {\r\n            value = &#039;&#039;;\r\n            options.expires = -1;   \r\n        }\r\n        var expires = &#039;&#039;;\r\n        if (options.expires &amp;&amp; (typeof options.expires == &#039;number&#039; || options.expires.toUTCString)) {\r\n            var date;\r\n            if (typeof options.expires == &#039;number&#039;) {\r\n                date = new Date();\r\n                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));\r\n            } else {\r\n                date = options.expires;\r\n            }\r\n            expires = &#039;; expires=&#039; + date.toUTCString();\r\n        }\r\n        var path = options.path ? &#039;; path=&#039; + (options.path) : &#039;&#039;;\r\n        var domain = options.domain ? &#039;; domain=&#039; + (options.domain) : &#039;&#039;;\r\n        var secure = options.secure ? &#039;; secure&#039; : &#039;&#039;;\r\n        document.cookie = [name, &#039;=&#039;, encodeURIComponent(value), expires, path, domain, secure].join(&#039;&#039;);\r\n    } else { \r\n        var cookieValue = null;\r\n        if (document.cookie &amp;&amp; document.cookie != &#039;&#039;) {\r\n            var cookies = document.cookie.split(&#039;;&#039;);\r\n            for (var i = 0; i &lt; cookies.length; i++) {\r\n                var cookie = jQuery.trim(cookies[i]);\r\n                if (cookie.substring(0, name.length + 1) == (name + &#039;=&#039;)) {\r\n                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\r\n                    break;\r\n                }\r\n            }\r\n        }\r\n        return cookieValue;\r\n    }\r\n};\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<h4>JQuery.cookie.js \u5e38\u7528\u65b9\u6cd5\uff1a<\/h4>\n<p><strong>1\u3001\u65b0\u5efa\u5e76\u8bbe\u7f6ecookie\u7684\u503c<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u65b0\u5efa\u5e76\u8bbe\u7f6ecookie\u7684\u503c *\/\r\n$.cookie(\"hiheng\";, \"www.hiheng.com\");\r\n\/\/\r\n<\/pre>\n<p><strong>2\u3001\u8bfb\u53d6Cookie\u503c<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u8bfb\u53d6Cookie\u503c, \u8f93\u51fa&quot;www.hiheng.com&quot; *\/\r\n$.cookie(\"hiheng\"); \r\n\/\/\r\n<\/pre>\n<p><strong>3\u3001\u8bbe\u7f6ecookie\u7684\u6709\u6548\u671f\uff0cexpires\u5c5e\u6027<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u8bbe\u7f6ecookie\u7684\u6709\u6548\u671f\u4e00\u5468\u3002 *\/\r\n$.cookie(\"hiheng\", \"www.hiheng.com\",{expires: 7}); \r\n\/\/\r\n<\/pre>\n<p><strong>4\u3001\u8bbe\u7f6ecookie\u7684\u57df\u540d\u8303\u56f4\uff0cdomain\u5c5e\u6027<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u8bbe\u7f6ecookie\u7684\u57df\u540d\u8303\u56f4\uff0chiheng.com *\/\r\n$.cookie(\"hiheng\", \"www.hiheng.com\", {domain: \"hiheng.com\"});\r\n\/\/\r\n<\/pre>\n<p><strong>5\u3001\u8bbe\u7f6ecookie\u7684\u6709\u6548\u8def\u5f84\uff0cpath\u5c5e\u6027<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u65b0\u5efa\u4e00\u4e2acookie \u5305\u62ec\u6709\u6548\u671f \u8def\u5f84 \u57df\u540d\u7b49 *\/\r\n$.cookie(\"hiheng\", \"www.hiheng.com\", {path: \"\/\"});\r\n\/\/\r\n<\/pre>\n<p><strong>6\u3001\u7efc\u5408\u8bbe\u7f6ecookie\u7684\u5404\u79cd\u5c5e\u6027<\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u65b0\u5efa\u4e00\u4e2acookie \u5305\u62ec\u6709\u6548\u671f \u8def\u5f84 \u57df\u540d\u7b49 *\/\r\n$.cookie(\"hiheng\", \"www.hiheng.com\", {expires: 7, path: \"\/\", domain: \"hiheng.com\", secure: true});\r\n\/\/\r\n<\/pre>\n<p><strong>7\u3001\u5220\u9664\u4e00\u4e2acookie <\/strong><\/p>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\/\/\r\n\/* \u5220\u9664\u4e00\u4e2aname\u4e3a\u201dhiheng\u201c\u7684cookie *\/\r\n$.cookie(\"hiheng\", null); \r\n\/\/\r\n<\/pre>\n<h4>jquery.cookie.js\u6e90\u7801\uff0c<a href=\"http:\/\/www.hiheng.com\/jquery\/jquery.cookie.js\" target=\"_blank\">\u4e0b\u8f7d<\/a><\/h4>\n<pre class=\"brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\njQuery.cookie = function(name, value, options) {\r\n    if (typeof value != &#039;undefined&#039;) { \r\n        options = options || {};    \r\n        if (value === null) {\r\n            value = &#039;&#039;;\r\n            options.expires = -1;   \r\n        }\r\n        var expires = &#039;&#039;;\r\n        if (options.expires &amp;&amp; (typeof options.expires == &#039;number&#039; || options.expires.toUTCString)) {\r\n            var date;\r\n            if (typeof options.expires == &#039;number&#039;) {\r\n                date = new Date();\r\n                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));\r\n            } else {\r\n                date = options.expires;\r\n            }\r\n            expires = &#039;; expires=&#039; + date.toUTCString();\r\n        }\r\n        var path = options.path ? &#039;; path=&#039; + (options.path) : &#039;&#039;;\r\n        var domain = options.domain ? &#039;; domain=&#039; + (options.domain) : &#039;&#039;;\r\n        var secure = options.secure ? &#039;; secure&#039; : &#039;&#039;;\r\n        document.cookie = [name, &#039;=&#039;, encodeURIComponent(value), expires, path, domain, secure].join(&#039;&#039;);\r\n    } else { \r\n        var cookieValue = null;\r\n        if (document.cookie &amp;&amp; document.cookie != &#039;&#039;) {\r\n            var cookies = document.cookie.split(&#039;;&#039;);\r\n            for (var i = 0; i &lt; cookies.length; i++) {\r\n                var cookie = jQuery.trim(cookies[i]);\r\n                if (cookie.substring(0, name.length + 1) == (name + &#039;=&#039;)) {\r\n                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\r\n                    break;\r\n                }\r\n            }\r\n        }\r\n        return cookieValue;\r\n    }\r\n};\r\n\r\n\r\n<\/pre>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[151,11],"tags":[127,15,14],"class_list":["post-1806","post","type-post","status-publish","format-standard","hentry","category-javascript","category-11","tag-cookie","tag-javascript","tag-jquery"],"_links":{"self":[{"href":"http:\/\/www.hiheng.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.hiheng.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hiheng.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hiheng.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hiheng.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1806"}],"version-history":[{"count":18,"href":"http:\/\/www.hiheng.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1806\/revisions"}],"predecessor-version":[{"id":1824,"href":"http:\/\/www.hiheng.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1806\/revisions\/1824"}],"wp:attachment":[{"href":"http:\/\/www.hiheng.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hiheng.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1806"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hiheng.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}