`
FireFlyWithCat
  • 浏览: 87155 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

关于jquery插件cookies的使用

阅读更多
下面就是一个例子:不过没有设置 domain、path、hoursToLive……几个属性,所以一旦关闭浏览器就不会保存cookie了。
<html>
<head>
<script type="text/javascript" src="jquery.js"></script> //插入js
<script type="text/javascript" src="jquery.cookies.2.2.0.min.js"></script>//插入插件。注意:这个插件一定要放在jquery文件的后面,否则不会生效
<script type="text/javascript">
$(document).ready(function(){
$(".tijiao").click(function(){   //触发button事件
$.cookies.set('name',$(".userName").value);
$.cookies.set('password',$(".password").value);   //把表单中得到的value存入cookie
alert("已经存入cookie");
});
var userN=$.cookies.get( 'name' );  
var userP=$.cookies.get( 'password' ); //从cookie中读取数据
if(userN!=""&&userN!=null&&userP!=""&&userP!=null)
{$(".userName").value=userN;
$(".password").value=userP;   // 赋值
}
else{
alert("userName or password is not exist");};
})
</script>
</head>
<body>
<div>
<table>
<tr id="name">
<td><lable>用户名:</lable></td>
<td><input type="text" class="userName" value=""/></td>
</tr>
<tr>
<td><lable>密码:</lable></td>
<td><input type="password1" class="password" value=""/></td>
</tr>
<tr>
<td><input type="button" class="tijiao" value="登录"/></td>
</tr>
</table>
</div>
</body>
</html>

要想设置cookie的其他属性就看这个:
var newOptions = {
    domain: '*.mydomain.com',//设置域
    path: '/somedir',//设置路径
    expiresAt: new Date( 2011, 1, 1 ),//设置cookie失效时间
    hoursToLive:0.01//设置cookie存活时间
    secure: true//是否保护
  }

详情请查看:
http://code.google.com/p/cookies/wiki/Documentation#Test_if_browser_is_accepting_cookies
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics