npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-icookie

v1.0.3

Published

plugin for browser cookie

Readme

js-icookie

一个用来在浏览器(IE9+)中操作cookie的JavaScript Api; 还未经严密测试,发现bug请及时联系[email protected]

安装

用script标签引入

下载here并引入:

<script src="*/icookie.js"></script>

NPM

  $ npm i js-icookie -S

基本用法

全局配置

按照代码执行顺序,全局配置声明之后存入cookie才受全局配置影响

iCookie.config({
	path: '/test', //只在/test路径和test的子路径下可读,默认为'/',即在所有路径下可读;
	expires: '3y', //缓存有效期为3年,也可写成1y:1年、2m:2月、3d或者3(number):3天、4h:4小时,默认为浏览器退出后缓存自动清除;
	domain: '.che300.com'  //缓存的可读域,默认为当前域,如你在www.che300.com下使用默认配置存入缓存,domain默认为www.che300.com;则只有在www.che300.com域名下才可以访问该缓存,若要让dingjia.che300.com也能访问该缓存,domain应该设为.che300.com,注意前面有“.”;
});

存入(以下范例默认没有设置全局配置)

存入一个key,value对,path、domain,expires均为默认值;

iCookie.set('key', 'value');

存入一个key,value对,path,domain为默认值,有效期为7天;

iCookie.set('key', 'value', { expires: 7 });

亦可写为

iCookie.set('key', 'value', 7);

存入一个key,value对,path为'/test',domain为当前域,有效期为2个月;

iCookie.set('key', 'value', { expires: '2m', path: '/test' });

批量存,存入三个key,value对

iCookie.set({
	'key1': 'value1',
	'key2': 'value2',
	'key3': 'value3'
});

批量存带配置,存入三个key,value对,有效期为7天

iCookie.set({
	'key1': 'value1',
	'key2': 'value2',
	'key3': 'value3'
},{
	'expires': 7
});

顶替全局配置

其中key,value对的有效期为7天,path为/test,key1,value1对的有效期为3天,path为/

iCookie.config({
	'expires': 3,
	'path': '/test'
});

iCookie.set('key', 'value', {
	'expires': 7
});

iCookie.set('key1', 'value1',{
	'path': '/'
});

读取(以下范例默认没有设置全局配置)

读取里面有几个未解决的坑,等会说

单个读取

iCookie.set('key', 'value');
iCookie.get('key'); // => 'value'
iCookie.get('key1'); // => undefined

读取全部缓存

iCookie.get(); // => { key: 'value' }

读取多个缓存

iCookie.set({
	'key1':'value1',
	'key2':'value2'
})
iCookie.get(['key1','key2']); // => { 'key1': 'value1', 'key2': 'value2' }

iCookie.set('key1', '我是默认 /');
iCookie.set('key1', '我是设置 /test', {
	path: '/test'
});
iCookie.get('key1'); // => '我是默认 /'
iCookie.get(); // => {'key1': '我是默认 /'}

如果存入同名cookie,不同路径或不同域,则两者都会存在于cookie中,读取的时候只能读取到其中一个,至于为什么请看源码,我测试过的浏览器都是只能读取到离当前路径最远的路径,因为document.cookie返回的cookie字符串,当前路径的在最前面,后面的顶替了前面的

解决方案: 不要存入同名cookie,我有什么办法? 我也很绝望啊

设想: 2.0我可能会做成这样 =>

<!--设想start-->
iCookie.get(); // => {'/': {'key1': '我是默认 /'}, '/test': {'key1': '我是设置 /test'}}
<!--设想end-->

。。。。。。。。。。。。。。。这是另一个坑。。。。。。。。。。。。。。。。。。。

iCookie.set('key1', {'a': 1});
iCookie.get('key1'); // => '{"a": 1}'

存入对象进去,只能拿到json字符串

解决方案: 请自行JSON.parse

设想: 2.0我可能会做成这样 =>

<!--设想start-->
iCookie.get('key1'); // => {"a": 1}  直接返回对象
<!--设想end-->

其实存入非字符串进去,最后拿到的都是字符串,笑哭

删除(以下范例默认没有设置全局配置)

删除里面有也有几个未解决的坑,等会说

iCookie.set('key', 1);
iCookie.get('key'); // => 1
iCookie.remove('key');
iCookie.get('key'); // => undefined, 删除成功了
iCookie.set('key1', 'value1');
iCookie.set('key2', 'value2', { path: '/test' });
iCookie.remove('key1'); // 删除成功
iCookie.remove('key2'); // 删除失败,并不是每次删除失败都报错,如果当前domain或当前path找到了这个cookie,但是domain或者path设置的不一致,删除不成功也不报错,只有当前domain或当前path找不到这个cookie才报错;
iCookie.remove('key2', { path: '/test' }); // 删除成功 !

remove在源码内部还是走set,其实删除cookie就是把他的过期时间设为昨天,set的时候path和domain是怎样的配置,remove的时候path和domain必须是相同配置才能删除成功,如果当前domain或当前path找到了这个cookie,但是domain或者path设置的不一致,删除不成功也不报错,只有当前domain或当前path找不到这个cookie才报错;

作者

oxDesigner, 一个将近三十岁却依旧帅气的男人; 如果你发现了bug,请及时提交Issues,亦可联系[email protected]; 如果你用着舒服,请给我一颗star