<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="Scripts/jquery-1.8.2.min.js"></script>
<script src="Scripts/jquery.cookie.js"></script>
<script type="text/javascript">
$(function () {
$("#btnLogin").click(function () {
var strName = $("#txtName").val();
var strPwd = $("#txtPwd").val();
if (strName == "sa" && strPwd == "123") {
$.cookie("userName", strName, { expires: 1 });//没有设置失效时间,所以是缓存cookie,有设置失效时间则是硬盘cookie,expires单位是天
alert("登陆成功");
window.location.href = "11Index.html"; }
});
});
</script>
</head>
<body>
用户名:<input type="text" id="txtName" /><br />
密码:<input type="text" id="txtPwd" /><br />
<input type="button" id="btnLogin" value="登录" />
</body>
</html>

  登陆后的页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="Scripts/jquery-1.8.2.min.js"></script>
<script src="Scripts/jquery.cookie.js"></script>
<script type="text/javascript">
$(function () {
var strName = $.cookie("userName");
if (strName && strName != "")//判断既不为null也不为undefined 和不为空字符串
{
$('#userName').text();
} else {
alert("请先登录");
window.location = '10Cookie.html';
} });
//退出方法
function exit() {
$.cookie("userName", "", { expires: -1 });//设置过期
alert("退出成功");
}
</script>
</head>
<body>
首页,欢迎<span id="userName"></span>
<a href="javascript:exit()">退出登录</a>
</body>
</html>

  

10Cookie的更多相关文章

随机推荐

  1. &lt;源代码&gt;FTPclient追加方式上传自己定义信息

    实现功能:向FTPserver以追加方式上传自己定义信息(例程中为:2014-10-08 13:47:15 test.) 源代码下载(免积分):http://download.csdn.net/det ...

  2. Java 强引用,软引用,弱引用

    1.强引用 public void handleMessage(Message msg) { case FAIL: GoplayException mException = new GoplayExc ...

  3. Windows7中安装内存与可用内存不一致的解决办法

    转载:http://blog.sina.com.cn/s/blog_56741a420100h9d1.html 问题现象: 安装完Windows7后,在计算机->属性中,会看到安装内存.但有时在 ...

  4. (转)Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds

    仰天长啸 Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds... 当启动tomcat时候出现 S ...

  5. JavaScript的对象——灵活与危险

    转:http://www.cnblogs.com/tolg/p/4873000.html 没有哪种数据结构比JavaScript的对象更简单灵活了.作为一个弱动态类型语言,JavaScript对对象的 ...

  6. java web 之 WebRoot和WebContent目录

    WebRoot和WebContent都是程序的根文件夹,无本质区别,一下是两者的共同点和不同点: 共同点:都有一个WEB-INF文件夹,其下文件不可直接访问: WEB-INF是安全目录,所谓安全,就是 ...

  7. 设计模式--委托模式C++实现

    原文章地址:http://www.cnblogs.com/zplutor/archive/2011/09/17/2179756.html [委托模式 C++实现] 我对.Net的委托模型印象很深刻,使 ...

  8. 在world中批量调整图片的大小

    1.Alt+F8调出vb宏  创建一个宏名字,setsize 粘贴代码后保存关闭. Sub setsize() ' ' setsize 宏 ' ' Dim iSha As InlineShape Fo ...

  9. Redis 作为缓存服务器的配置

    随着redis的发展,越来越多的架构用它取代了memcached作为缓存服务器的角色,它有几个很突出的特点:1. 除了Hash,还提供了Sorted Set, List等数据结构2. 可以持久化到磁盘 ...

  10. eclipse中的debug的用法

    最基本的操作是: 1.首先在一个java文件中设断点,然后debug as-->open debug Dialog,然后在对话框中选类后--> Run 当程序走到断点处就会转到debug视 ...