1, rely on jquery library 2, browser compatibility situation
3. Download
Official Github:https://github.com/carhartl/jquery-cookie
4. Use
- Create an entire site cookie
$.cookie(‘name‘, ‘value‘);
- Create an entire site cookie that is valid for 7 days
$.cookie(‘name‘, ‘value‘, { expires: 7 });
- Create a cookie that is valid only for the
path
path page, and the cookie is valid for 7 days
$.cookie(‘name‘, ‘value‘, { expires: 7, path: ‘/‘ });
$.cookie(‘name‘); // 如果cookie存在 则获取到cookie值 => ‘value‘$.cookie(‘nothing‘); // 如果cookie不存在 则返回 => undefined
- Get all the visible cookies
$.cookie(); // 数据格式 => { name: ‘value‘ }
$.removeCookie(‘name‘); // => true$.removeCookie(‘nothing‘); // => false
- Delete a cookie with attributes
$.cookie(‘name‘, ‘value‘, { path: ‘/‘ });// 错误$.removeCookie(‘name‘); // => false// 正确$.removeCookie(‘name‘, { path: ‘/‘ }); // => true
5. Attribute domain
Create a domain name owned by the Web page where the cookie resides
$.cookie(‘name‘, ‘value‘, { domain: ‘weber.pub‘ });
Secure
The default is False if the transport protocol for True,cookie needs to be https;
$.cookie(‘name‘, ‘value‘, { secure: true });$.cookie(‘name‘); // => ‘value‘$.removeCookie(‘name‘, { secure: true });
Raw
The default is False, the read and write time is automatically encoded and decoded (using encodeURIComponent encoding, using decodeuricomponent decoding), turn off this function, set to TRUE.
$.cookie.raw = true;
Json
$.cookie.json = true;
Jquery.cookie Introduction and Usage