YouTip LogoYouTip

Jquery Cookie Plugin

# jQuery Cookie Plugin jQuery can manipulate cookies through the jquery.cookie.js plugin. **Official Address**: [http://plugins.jquery.com/cookie/](http://plugins.jquery.com/cookie/) **Github Address**: [https://github.com/carhartl/jquery-cookie](https://github.com/carhartl/jquery-cookie) Before using jquery.cookie.js, you need to include jQuery first: We can use third-party resource libraries to include these two files: * * * ## Usage **Create a cookie:** $.cookie('name', 'value'); If no expiration time is specified, it will expire when the browser is closed. **Create a cookie and set it to expire in 7 days:** $.cookie('name', 'value', { expires: 7 }); Create a cookie and set its valid path to the root directory of the website: $.cookie('name', 'value', { expires: 7, path: '/' }); **Note:** By default, only the page that set the cookie can read it. If you want a page to read a cookie set by another page, you must set the cookie's path. The cookie's path is used to set the top-level directory that can read the cookie. Setting this path to the root directory of the website allows all pages to read each other's cookies (generally not recommended to avoid conflicts). **Read a cookie:** $.cookie('name'); // => "value" $.cookie('nothing'); // => undefined **Read all cookie information:** $.cookie(); // => { "name": "value" } **Delete a cookie:** // Returns true if the cookie is deleted successfully, otherwise returns false $.removeCookie('name'); // => true $.removeCookie('nothing'); // => false // If path was used when writing, the same attributes (path, domain) must be used when reading $.cookie('name', 'value', {path: '/'}); // The following code will $.removeCookie('name'); // => false// The following code will $.removeCookie('name', {path: '/'}); // => true **Note:** When deleting a cookie, you must pass the exact same path, domain, and secure options used to set the cookie. ## Example $(document).ready(function(){ $.cookie('name', 'tutorial'); // Create cookie name = $.cookie('name'); // Read cookie $("#test").text(name); $.cookie('name2', 'tutorial2', {expires: 7, path: '/'}); name2 = $.cookie('name2'); $("#test2").text(name2); }); [Try it Β»](#) After execution, we can view the Cookie information in the browser, as shown in the following image: !(#) * * * ## Parameter Description ### raw Default value: false. By default, cookies are automatically encoded and decoded when read and written (using encodeURIComponent for encoding and decodeURIComponent for decoding). To disable this feature, set raw:true: $.cookie.raw = true; ### json Set cookie data to be stored and read using JSON, so you don't need to use JSON.stringify and JSON.parse. $.cookie.json = true; ### expires expires: 365 Defines the cookie's expiration time. The value can be a number (in days, calculated from the time the cookie is created) or a Date object. If omitted, the created cookie is a session cookie and will be deleted when the user exits the browser. ### path path: '/' Default: Only the page that set the cookie can read it. Defines the cookie's valid path. By default, the value of this parameter is the path of the page that created the cookie (standard browser behavior). If you want to access this cookie across the entire website, you need to set the valid path like this: path: '/'. If you want to delete a cookie that has a defined valid path, you need to include this path when calling the function: $.cookie('the_cookie', null,{ path: '/' }); ### domain domain: 'example.com' Default value: The domain of the page that created the cookie. ### secure secure: true Default value: false. If true, the cookie transmission requires a secure protocol (HTTPS).
← Cpp Pointer OperatorsCss Counters β†’