- Getting Started
- edpx-mobile v1.0.0
- rebas
- rider
- saber-ajax
- saber-cookie
- saber-dom
- saber-emitter
- saber-env
- saber-fastbutton
- saber-firework
- saber-firework v2.0.0
- saber-firework v2.0.0-beta.2
- saber-firework v2.0.0-beta.1
- saber-firework v2.0.0-alpha.2
- saber-firework v2.0.0-alpha.1
- saber-firework v1.0.0
- saber-firework v0.4.2
- saber-firework v0.4.1
- saber-firework v0.4.0
- saber-firework v0.3.2
- saber-firework v0.3.1
- saber-firework v0.3.0
- saber-firework v0.2.6
- saber-firework v0.2.5
- saber-firework v0.2.4
- saber-firework v0.2.1
- saber-firework v0.2.0
- saber-firework v0.1.0-beta.4
- saber-firework v0.1.0-beta.3
- saber-firework v0.1.0-beta.1
- saber-geo v0.1.0
- saber-lang
- saber-log v0.1.0
- saber-matchmedia
- saber-mm
- saber-promise
- saber-promise v2.1.0
- saber-promise v2.0.0
- saber-promise v2.0.0-beta.1
- saber-promise v2.0.0-alpha.3
- saber-promise v2.0.0-alpha.2
- saber-promise v2.0.0-alpha.1
- saber-promise v1.0.2
- saber-promise v1.0.0
- saber-promise v0.1.2
- saber-promise v0.1.2-beta.3
- saber-promise v0.1.2-beta.2
- saber-promise v0.1.2-beta.1
- saber-promise v0.1.1
- saber-promise v0.1.0
- saber-rainbow
- saber-router
- saber-router v2.0.0
- saber-router v2.0.0-beta.1
- saber-router v2.0.0-alpha.2
- saber-router v2.0.0-alpha.1
- saber-router v1.0.0
- saber-router v0.2.4
- saber-router v0.2.3
- saber-router v0.2.2
- saber-router v0.2.1
- saber-router v0.2.0
- saber-router v0.1.0
- saber-router v0.1.0-beta.5
- saber-router v0.1.0-beta.4
- saber-router v0.1.0-beta.3
- saber-router v0.1.0-beta.2
- saber-router v0.1.0-beta.1
- saber-run
- saber-scroll
- saber-storage
- saber-string
- saber-tap
- saber-uri
- saber-viewport
- saber-viewport v1.1.0
- saber-viewport v1.0.0
- saber-viewport v0.2.12
- saber-viewport v0.2.11
- saber-viewport v0.2.10
- saber-viewport v0.2.9
- saber-viewport v0.2.8
- saber-viewport v0.2.6
- saber-viewport v0.2.5
- saber-viewport v0.2.1-beta.7
- saber-viewport v0.2.1-beta.6
- saber-viewport v0.2.1-beta.5
- saber-viewport v0.2.1-beta.4
- saber-viewport v0.2.1-beta.3
- saber-viewport v0.2.1-beta.2
- saber-viewport v0.2.1-beta.1
- saber-viewport v0.2.0
- saber-viewport v0.1.0
- saber-widget
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
saber-cookie v1.0.0
适合移动端的Cookie封装
Installation
通过 edp 引入模块:
edp import saber-cookie
Usage
require('saber-cookie', function(Cookie) { // create a cookie (page-session) Cookie.set('__saber_test', 'saber'); // get a cookie with given name var val = Cookie.get('__saber_test'); console.info(val); // create a cookie with expires (1 day) Cookie.set( '__saber_1d', '1day', {expires: 1 * 24 * 60 * 60 * 1000} ); console.info(Cookie.get('__saber_1d')); // create a cookie with path Cookie.set( '__saber_root', 'root', {path: '/'} ); console.info(Cookie.get('__saber_root')); // create a cookie with domain Cookie.set( '__saber_host', {domain: document.domain} ); console.info(Cookie.get('__saber_host')); // create a cookie with raw value Cookie.set( '__saber_raw', 'hello, saber', {raw: true} ); console.info( 'decoded: %s , raw: %s', Cookie.get('__saber_raw'), Cookie.get('__saber_raw', {raw: true}) ); // remove a cookie Cookie.set('__saber_remove', 'xx'); var beforeVal = Cookie.get('__saber_remove'); Cookie.remove('__saber_remove'); var afterVal = Cookie.get('__saber_remove'); console.info('before: %s , after: %s', beforeVal, afterVal); });
Methods
get(name [, options])
获取键名为 name
的 cookie 值,若 cookie 不存在或 name
为空,则返回 null
- name
{string}
cookie的键名 - options
{Object}
参数可选,支持的配置项:- raw
{boolean}
是否不自动解码(decodeURIComponent
), 为true
时会获取未经过解码的cookie原始存储值
- raw
- return
{string}
// 获取键名为 mycookie 的 cookie 值 Cookie.get('mycookie'); // 获取键名为 rawcookie 的未解码的 cookie 存储值 Cookie.get('rawcookie', {raw: true});
set(name, value[, options])
设置键名为 name
,值为 value
的新 cookie
- name
{string}
cookie的键名 - value
{string}
cookie的原始值 - options
{Object}
参数可选,支持的配置项:- expires
{Date|Number}
cookie的过期时间, 为数字时单位为毫秒
- domain
{string}
cookie的域名 - path
{string}
cookie路径 - secure
{boolean}
cookie是否安全传输 - raw
{boolean}
是否不自动编码(encodeURIComponent
), 为true
时参数value
会以未编码的原始值存储
- expires
- return
{void}
// 默认(session级)的新cookie Cookie.set('test1', 'session cookie'); // 设置了有1天效期的新cookie Cookie.set('test2', 'baidu', { expires: 1 * 24 * 60 * 60 * 1000 }); // 设置了路径的新cookie Cookie.set('test3', 'baidu', {path: '/'}); // 设置了域名的新cookie Cookie.set('test4', 'baidu', {domain: 'baidu.com'}); // 设置了安全传输的新cookie Cookie.set('test5', 'baidu', {secure: true}); // 禁用自动编码(encodeURIComponent)的cookie Cookie.set('test6', 'hello, saber', {raw: true});
remove(name[, options])
删除键名为 name
的 cookie
- name
{string}
cookie的键名 - options
{Object}
参数可选,支持的配置项:- domain
{string}
cookie的域名 - path
{string}
cookie路径 - secure
{boolean}
cookie是否安全传输
- domain
- return
{void}
// 删除键名为 mycookie 的 cookie Cookie.remove('mycookie'); // 删除 `baidu.com` 域下,路径为 `/` 的键名为 `othercookie` 的 cookie Cookie.remove('othercookie', {domain: 'baidu.com', path: '/'});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论