用jStore代替Cookies实现客户端存储

2010 二月 7
by Luin

开发网站时有时需要把一些数据存储到客户端,一般来讲我们都会使用小甜饼(cookies)来完成这一操作,但是小甜饼不一定总是很好吃:

  1. 每个cookies的大小不能超过4096字节。(没关系,我只存很少东西)
  2. 浏览器一般至多允许每个网站使用20个小甜饼。(浏览器是不接受过度贿赂的)
  3. 一些严厉的浏览器要求所有网站总共的甜饼数不能超过300个。(存cookies都得拼RP)
  4. 最好玩的:小甜饼不能跨浏览器!(好吧,我受够了…)

于是,有些场合cookies便无法满足要求了。

幸运的是,客户端存储还有更多更好的解决方案!

  1. UserData : 只支持IE 7,Cool吧 -_-!
  2. Google Gear:支持所有你找得到的浏览器!但是你得安装Google Gears…
  3. Flash:同Google Gear,只不过装Flash插件即可
  4. HTML5提供了一些本地存储方案,但…别指望了

从上面来看比较好的解决方案应该是Flash了,很少有浏览器不支持(安装)flash。

jQuery的jStore插件则提供本地存储的支持。

下面来个示例(YUI也提供了类似的插件):

我们要实现的功能是一个投票系统(为了简化问题,PHP将不会出场:-( ),对于某个技术,用户可以选择自己的级别(想学、菜鸟、老鸟),选择后将无法再次选择。

HTML代码:

...
<script src="lib/jquery.js" type="text/javascript"></script>
<script src="lib/jStore/jquery.jstore-all.js" type="text/javascript"></script>
<script src="custom.js" type="text/javascript"></script>
...
 
<div id="votes" class="votes-php">
        <a onclick="return vote(1);" href="/">想学</a>
        <a onclick="return vote(2);" href="/">菜鸟</a>
        <a onclick="return vote(3);" href="/">老鸟</a>
</div>
...

其中href链接应该指到一个可以投票的php链接以防止用户的浏览器不支持javascript(而我的方案是对于不支持javascript的浏览器便不允许投票)。

custom.js

//当前技术
var tech;
// First, we must tell jStore where to find the HTMLfile which embeds our flash script.
// If you move the jStore.swf or jStore.Flash.html files out of their default directories (document root),
// use this variable to define where to access the .html file. Within the .html file, you can set up the
// path to the .swf file.
// Then, set up the default engine we wish to use.
jQuery.extend(jQuery.jStore.defaults, {
	project: 'Luin-Demo',
	engine: 'flash',
	flash: '/scripts/lib/jStore/jStore.Flash.html'
}) 
 
// Next, we need to wait for jStore to prepare the storage engine
jQuery.jStore.ready(function(engine){
	jQuery.jStore.flashReady(function(){
		// Finally, we need to wait for the storage engine to be ready.
		// Once this function is called, you can use the jStore interface synchronously
		engine.ready(function(){
			var engine = this; 
 
			tech = $('#votes').attr('class').replace('votes-', '');
 
			var hh = engine.get(tech);
			if(hh) {hideVoteCdLink(hh);}
		})
	})
});  
 
// Finally, we trigger jStore's load function. This is a new step in the
// activation procedure of jStore, since Version 1.1
$(function(){
	//!!!
	jQuery.jStore.load();
}); 
 
function vote(v)
{
    //$.get('/technology/mark?type=nd&amp;t=' + tech + '&amp;value=' + v);
    hideVoteCdLink(v);
    jQuery.jStore.set(tech, v);
	return false;
}
 
function hideVoteCdLink(v)
{
	var cd;
	if(v == 1) cd = "想学";
	else if(v == 2) cd = "菜鸟";
	else if(v == 3) cd = "老鸟";
	$('#votes').html(cd + ' , <a onclick="return changeVoteCdLink()" href="#">修改</a>');
}
function changeVoteCdLink()
{
	$('#votes').html('<a onclick="return vote(1);" href="/">想学</a> <a onclick="return vote(2);" href="/">菜鸟</a> <a onclick="return vote(3);" href="/">老鸟</a> ');
	return false;
}

这样可以看到,用户使用Firefox投票后,再用IE看也是投票后的样子,解决了!(注意JS代码中的//!!!处,如果客户端不支持flash的话,jQuery.jStore.load(); 会抛出异常,你应该try/catch一下并做处理(比如不允许投票或提示安装flash(如果是iPhone的话…) ) )

当然,这只是个没有使用价值的demo,没有数据库就没有一切。

jStore的官方网站:http://eric.garside.name/docs.html?p=jstore

相关日志

7 Responses leave one →
  1. 二月 7, 2010

    很好很强大。。。我很好奇这数据到底储存在哪里。。。貌似js不支持native io吧?。。。

    • Luin permalink*
      二月 7, 2010

      js不行,所以得flash上,flash可以在本地存取数据。
      应该存在Application Data的flash player目录里。

  2. Blankwonder permalink
    二月 22, 2010

    用iPhone以及准备买iPad的飘过………………

    • Luin permalink*
      二月 22, 2010

      我正准备买3gs呢…我还是喜欢kindle…

  3. yenn permalink
    五月 10, 2010

    你好,Google上发现你的这个真是我需要实现的jstore投票功能。。。能否打包发我一份吗?

    邮箱地址为:yeyongyi@yq1001.cn

    万分感谢

    • 五月 11, 2010

      文末注明了jStore的官网,到那儿下载吧。

  4. webui permalink
    九月 4, 2010

    这是一个好东西,我想用它当作缓存来。可以把这个项目发给我
    webui@139.com

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS