$.cookie usage

Source: Internet
Author: User

One: Introduce plug-in jQuery.cookie.js

/*!
* JQuery Cookie Plugin v1.4.1
* Https://github.com/carhartl/jquery-cookie
*
* Copyright Klaus Hartl
* Released under the MIT license
*/
(function (Factory) {
if (typeof define = = = ' function ' && define.amd) {
Amd
define ([' jquery '], factory);
} else if (typeof exports = = = ' object ') {
CommonJS
Factory (Require (' jquery '));
} else {
Browser Globals
Factory (JQuery);
}
} (function ($) {

var pluses =/\+/g;

function encode (s) {
Return Config.raw? S:encodeuricomponent (s);
}

function decode (s) {
Return Config.raw? S:decodeuricomponent (s);
}

function Stringifycookievalue (value) {
return encode (Config.json? Json.stringify (value): String (value));
}

function Parsecookievalue (s) {
if (S.indexof (' "') = = = 0) {
This was a quoted cookie as according to RFC2068, unescape ...
s = S.slice (1,-1). replace (/\\ "/g, '" '). Replace (/\\\\/g, ' \ \ ');
}

try {
Replace server-side written pluses with spaces.
If we can ' t decode the cookie, ignore it, it ' s unusable.
If we can ' t parse the cookie, ignore it, it ' s unusable.
s = decodeuricomponent (S.replace (pluses, "));
Return Config.json? Json.parse (s): s;
} catch (e) {}
}

function read (s, converter) {
var value = Config.raw? S:parsecookievalue (s);
Return $.isfunction (Converter)? Converter (value): value;
}

var config = $.cookie = function (key, value, options) {

Write

if (value!== undefined &&!$.isfunction (value)) {
Options = $.extend ({}, Config.defaults, options);

if (typeof options.expires = = = ' number ') {
var days = options.expires, t = options.expires = new Date ();
T.settime (+t + days * 864e+5);
}

return (Document.cookie = [
Encode (key), ' = ', Stringifycookievalue (value),
Options.expires? ‘; Expires= ' + options.expires.toUTCString (): ',//Use expires attribute, Max-age are not supported by IE
Options.path? ‘; Path= ' + options.path: ',
Options.domain? ‘; Domain= ' + options.domain: ',
Options.secure? ‘; Secure ': '
].join ("));
}

Read

var result = key? Undefined: {};

To prevent the as loop in the first place assign an empty array
In case there is no cookies at all. Also prevents odd result when
Calling $.cookie ().
var cookies = Document.cookie? Document.cookie.split ('; ‘) : [];

for (var i = 0, L = cookies.length; i < L; i++) {
var parts = cookies[i].split (' = ');
var name = decode (Parts.shift ());
var cookie = parts.join (' = ');

if (key && key = = = Name) {
If second argument (value) is a function it ' s a converter ...
result = Read (cookie, value);
Break
}

Prevent storing a cookie that we couldn ' t decode.
if (!key && (cookie = read (cookie))!== undefined) {
Result[name] = cookie;
}
}

return result;
};

Config.defaults = {};

$.removecookie = function (key, options) {
if ($.cookie (key) = = = undefined) {
return false;
}

Must not alter options, thus extending a fresh object ...
$.cookie (Key, "", $.extend ({}, Options, {Expires:-1}));
return!$.cookie (key);
};

}));

Two: Get the current window

  var webUrl = window.location.href;


Three: Initialize cookies:

$.cookie (' WebUrl ', ', {expires:-1});   //Where-1 means to initialize the delete cookie  if you want to access this cookie throughout the site, you need to set a valid path: path: '/'. {}

  


Four: Assigning values

     $.cookie (' WebUrl ', weburl,null);     Value-Assigned Cookie

  

Five: Value

var webUrl = $.cookie (' WebUrl '); Top.navTab.openTab (' _blank ', weburl,{title: ' License handling ', Fresh: ' true ', external: ' True '});

  

$.cookie usage

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.