jquery.cookie() 方法的使用(读取、写入、删除)
<script type="text/javascript" src="js/jquery.cookie.js"></script> 
1、会话cookie:
$.cookie('the_cookie', 'the_value');
当没有指明 cookie有效时间时,所创建的cookie有效期默认到用户关闭浏览器为止,所以被称为会话cookie;
 
2.创建一个cookie并设置有效时间为 7天: 
$.cookie('the_cookie', 'the_value', { expires: 7 }); 
当指明了cookie有效时间时,所创建的cookie被称为“持久 cookie (persistent cookie)”。
 
3.创建一个cookie并设置 cookie的有效路径:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
注:在默认情况下,只有设置 cookie的网页才能读取该 cookie。如果想让一个页面读取另一个页面设

置的cookie,必须设置cookie的路径。cookie的路径用于设置能够读取 cookie的顶级目录。将这

个路径设置为网站的根目录,可以让所有网页都能互相读取 cookie (一般不要这样设置,防止出现冲突) 。

 
4.读取cookie: 
$.cookie('the_cookie'); // cookie存在 => 'the_value' 
$.cookie('not_existing'); // cookie不存在 => null 
 
5.删除cookie,通过传递null作为cookie的值即可:
$.cookie('the_cookie', null); 
关于记住密码这个功能的实现:
一个页面简单的登录页面:
<form action="xxx/user/signIn" method="post" >
<input type="text" id="username" name="username" placeholder="请输入用户名" />
<input type="password" id="password" name="password" placeholder="请输入密码" />
<input type="checkbox" id="remember"> 记住用户名和密码
<button type="submit" class="btn btn-success" id="btn_login">登 录</button>
<button type="reset" class="btn btn-default">重 置</button>
</form> $(document).ready(function(){
//页面初始化时,如果是记住密码了,给输入框赋值。
if($.cookie("remember") == "true"){
$("#remember").prop("checked", true);
$("#username").val($.cookie("username"));
$("#password").val($.cookie("password"));
} //点击确定按钮,将记录状态信息存在cookie,同时submit;
$("#btn_login").on("click",function(){
remember();
return true;
}); //记录用户名密码。以及是否要记住。保存在cookie;
function remember(){
var checked = $("#remember").prop("checked");
if(checked == "checked" || checked) {
var username = $("#username").val();
var password = $("#password").val();
$.cookie("remember", "true", {expires: 30});
$.cookie("username", username, {expires: 30});
$.cookie("password", password, {expires: 30});
}else{
$.cookie("remember", "false", {expires: -1});
$.cookie("username", "", {expires: -1});
$.cookie("password", "", {expires: -1});
}
} //监听记住选择框是否选中,如果未选中,cookie中则不记录。
$("#remember").on("change", function(){
var checked = $("#remember").prop("checked");
if(checked == "checked" || checked) { }else{
$.cookie("remember", "false", {expires: -1});
$.cookie("username", "", {expires: -1});
$.cookie("password", "", {expires: -1});
}
});
});

jquery.cookie.js $.cookie()是怎么使用的更多相关文章

  1. 管理Cookie的插件——jquery.cookie.js

    下载地址:http://plugins.jquery.com/cookie/ jquery.cookie中的操作: 一.创建cookie: 1.创建一个会话cookie: $.cookie('cook ...

  2. jquery.cookie.js 的使用

    jquery.cookie.js 对cookie的操作 $.cookie('the_cookie'); //读取Cookie值 $.cookie('the_cookie', 'the_value'); ...

  3. 分享jquery.cookie.js

    代码如下: /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Copyright 20 ...

  4. 【jq】插件—缓存jquery.cookie.js

    jquery.cookie.js插件   轻量级cookie管理 1°下载地址:http://plugins.jquery.com/cookie/ 2°引入方式:(基于jquery) <scri ...

  5. JavaScript-Tool:jquery.cookie.js

    ylbtech-JavaScript-Tool:jquery.cookie.js 1.返回顶部 1.jquery.cookie.js /*! * jQuery Cookie Plugin v1.4.0 ...

  6. Juery插件-- jquery.cookie.js

    1.引入jquery <script src="scripts/jquery-1.8.8.js" type="text/javascript">&l ...

  7. jquery中的cookie

    关于cookie,一直是个很敏感的问题,以前对于cookie的处理,都是用原生的方式处理,创建函数对cookie进行处理,创建,设置以及删除.. function setCookie(key,valu ...

  8. jquery中的cookie操作

    使用前在页面中引入下面的代码 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Cop ...

  9. jquery和js cookie的使用解析

    JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的.而cookie是运行在客户端的,所以可以用JS来设置cookie. 在这里分别通过 ...

  10. jquery库的cookie用法

    一个完整设置与读取cookie的页面代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

随机推荐

  1. vue项目启动

    这篇文章主要用于有源码vue项目安装: 1.安装node.js环境(npm包管理器)前面博客有写到如何安装: 2.vue-cli 脚手架构建工具前面博客有写到如何安装: 3.cnpm  npm的淘宝镜 ...

  2. Redis (一) 概念安装

    一.阿里云安装Redis 1.安装Redis yum -y install redis 2.启动Redis service redis start 或者(推荐使用) systemctl start  ...

  3. C++ bitset

    itset存储二进制数位. bitset就像一个bool类型的数组一样,但是有空间优化——bitset中的一个元素一般只占1 bit,相当于一个char元素所占空间的八分之一. bitset中的每个元 ...

  4. first head in html 笔记

    目录: 1.了解HTML 2.学会简单构建网页 3.学会将单个的网页放在web上,组成网站 5.学会使用图片 6.严格HTML规范 7.HTML->XHTML 8.学会一点CSS样式 9.了解盒 ...

  5. utf-8和Unicode的区别

    链接 utf-8和Unicode到底有什么区别?是存储方式不同?编码方式不同?它们看起来似乎很相似,但是实际上他们并不是同一个层次的概念 要想先讲清楚他们的区别,首先应该讲讲Unicode的来由. 众 ...

  6. 使用kbmMW#1轻松实现REST

    使用kbmMW很容易创建REST服务器. 首先,我们制作服务器应用程序(或服务......取决于您). 在这种情况下,我们将添加一个简单的Form,为我们的kbmMW组件提供GUI和位置. 在Delp ...

  7. DialogFragment详解

    详解一: Android提供alert.prompt.pick-list,单选.多选,progress.time-picker和date-picker对话框,并提供自定义的dialog.在Androi ...

  8. 《TensorFlow实战》读书笔记(完结)

    1 TensorFlow基础 ---1.1TensorFlow概要 TensorFlow使用数据流图进行计算,一次编写,各处运行. ---1.2 TensorFlow编程模型简介 TensorFlow ...

  9. Ubuntu终端点击确定按钮的方法

    Ubuntu终端里出现需要点击 确定 按钮的时候,直接鼠标点击 确定 是不生效的,这个时候需要利用tab键选中这个 确定 按钮,然后回车键就可以了.

  10. 3.3 shell控制流结构

    shell中的控制流包括if then else语句,case语句,for循环,until循环,while循环,break控制,continue控制. 条件测试: 有时判断字符串是否相等或检查文件状态 ...