javascript设置和获取cookie的方法实例详解
作者:taozi165 发布时间:2024-04-22 13:05:08
标签:javascript,cookie
本文实例讲述了javascript设置和获取cookie的方法。分享给大家供大家参考,具体如下:
1. 设置cookie
function setCookie(cookieName,cookieValue,cookieExpires,cookiePath)
{
cookieValue = escape(cookieValue);//编码latin-1
if(cookieExpires=="")
{
var nowDate = new Date();
nowDate.setMonth(nowDate.getMonth()+6);
cookieExpires = nowDate.toGMTString();
}
if(cookiePath!="")
{
cookiePath = ";Path="+cookiePath;
}
document.cookie= cookieName+"="+cookieValue+";expires="+cookieExpires+cookiePath;
}
2. 获取cookie
function getCookieValue(cookieName)
{
var cookieValue = document.cookie;
var cookieStartAt = cookieValue.indexOf(""+cookieName+"=");
if(cookieStartAt==-1)
{
cookieStartAt = cookieValue.indexOf(cookieName+"=");
}
if(cookieStartAt==-1)
{
cookieValue = null;
}
else
{
cookieStartAt = cookieValue.indexOf("=",cookieStartAt)+1;
cookieEndAt = cookieValue.indexOf(";",cookieStartAt);
if(cookieEndAt==-1)
{
cookieEndAt = cookieValue.length;
}
cookieValue = unescape(cookieValue.substring(cookieStartAt,cookieEndAt));//解码latin-1
}
return cookieValue;
}
例子:
<!doctype html>
<html>
<head>
<title>cookie</title>
<meta charset="utf-8">
<script language="javascript" type="text/javascript">
//获取cookie
function getCookieValue(cookieName)
{
var cookieValue = document.cookie;
var cookieStartAt = cookieValue.indexOf(""+cookieName+"=");
if(cookieStartAt==-1)
{
cookieStartAt = cookieValue.indexOf(cookieName+"=");
}
if(cookieStartAt==-1)
{
cookieValue = null;
}
else
{
cookieStartAt = cookieValue.indexOf("=",cookieStartAt)+1;
cookieEndAt = cookieValue.indexOf(";",cookieStartAt);
if(cookieEndAt==-1)
{
cookieEndAt = cookieValue.length;
}
cookieValue = unescape(cookieValue.substring(cookieStartAt,cookieEndAt));//解码latin-1
}
return cookieValue;
}
//设置cookie
function setCookie(cookieName,cookieValue,cookieExpires,cookiePath)
{
cookieValue = escape(cookieValue);//编码latin-1
if(cookieExpires=="")
{
var nowDate = new Date();
nowDate.setMonth(nowDate.getMonth()+6);
cookieExpires = nowDate.toGMTString();
}
if(cookiePath!="")
{
cookiePath = ";Path="+cookiePath;
}
document.cookie= cookieName+"="+cookieValue+";expires="+cookieExpires+cookiePath;
}
//页面加载时间处理函数
function window_onload()
{
var userNameElem = document.getElementById("userName");//用户名输入框对象
var passwordElem = document.getElementById("password");//密码输入框对象
var currUserElem = document.getElementById("currUser");//复选框对象
var currUser = getCookieValue("currUser");
if(currUser!=null)
{
userNameElem.value=currUser;
currUserElem.checked = true;
}
if(userNameElem.value!="")
{
passwordElem.focus();//密码输入框获得焦点
}
else
{
currUserElem.focus();//用户名输入框获得焦点
}
}
//表单提交处理
function login()
{
var userNameElem = document.getElementById("userName");
var passwordElem = document.getElementById("password");
var currUserElem = document.getElementById("currUser");
if(userNameElem.value=="" || passwordElem.value=="")
{
alert("用户名或密码不能为空!");
if(userNameElem.value=="")
{
userNameElem.focus();//用户名输入框获得焦点
}
else
{
passwordElem.focus();//密码输入框获得焦点
}
return false;
}
if(currUserElem.checked)
{
setCookie("currUser",userNameElem.value,"","");//设置cookie
}
else
{
var nowDate = new Date();//当前日期
nowDate.setMonth(nowDate.getMonth()-2);//将cookie的过期时间设置为之前的某个日期
cookieExpires = nowDate.toGMTString();//过期时间的格式必须是GMT日期的格式
setCookie("userName","",cookieExpires,"");//删除一个cookie只要将过期时间设置为过去的一个时间即可
}
return true;
}
</script>
<style type="text/css">
div{
font-size:12px;
}
</style>
</head>
<body onload="window_onload()">
<div>
<form id="loginForm" onsubmit="return login()">
用户名:<input type="text" id="userName"><br>
密 码:<input type="password" id="password">
<input type="checkbox" id="currUser">记住用户名<br>
<input type="submit" value="登录">
</form>
</div>
</body>
</html>
注意:
由于google Chrome浏览器为了安全只支持online-cookie,所以在本地测试时是没有效果的,需要上传到服务器试一下。
希望本文所述对大家JavaScript程序设计有所帮助。


猜你喜欢
- 1、问题描述在使用v-model指令实现输入框数据双向绑定,输入值时对应的这个变量的值也随着变化;但是这里不允许使用v-model,需要写一
- 前言之前说了怎么写机器码到内存,然后调用。现在说说怎么优化。用Python发送微信消息给好友第二次优化再看一遍c语言的代码void Send
- 我就废话不多说啦!dpi=1 600×400dpi=21200×800dpi=31800×1200........dpi=21(21×600
- 主要用到 str.charCodeAt()和 String.fromCharCode()方法--》使用 charCodeAt() 来获得字符
- 要做一个页面上短信息的提示音的功能,本来想用HTML5中Audio+IE下的bgsound来实现,可是发现每种浏览器对Audio的解码类型还
- 说说最近的一个案例吧,线上阿里云RDS上的一个游戏日志库最近出现了一点问题,随着游戏人数的增加,在线日
- 注意: 在搭建网络的时候用carpool2D的时候,让高度和宽度方向不同池化时,用如下:nn.MaxPool2d(kernel_size=2
- 切片:方便截取list、tuple、字符串部分索引的内容正序切片语法:dlist = doList[0:3]表示,从索引0开始取,直到索引3
- python运行问题Traceback (most recent call last)出现报错traceback(most recent c
- Spring事务管理事务(Transaction),一般是指要做的或所做的事情。在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序
- 本文实例讲述了Python wxPython库Core组件BoxSizer用法。分享给大家供大家参考,具体如下:wx.BoxSizer:bo
- 昨天在写“同IP站点查询”工具的时候,需要先用ASP获取查询域名的IP,本来是用WSHSHELL组件,代码如下:<%@LANGUAGE
- golang用来序列化的模块有很多,我们来介绍3个。json首先登场的是json,这个几乎毋庸置疑。序列化package mainimpor
- 导读:分析时间序列数据的一种简单而有效的方法就是将时间序列数据可视化在一个图表上,这样我们就可以从中推断出某些假设。本文将以股价数据集为例,
- 第一步:python解释器,到网上下载安装下就行。网址:https://www.python.org/downloads/windows/值
- 在python的时间使用时,我们无非就是输出字符串的形式,又或者是其他的形式跟字符串之间的来回转换。时间数组对于我们获取具体的年或是天数,都
- 目录一、善用组件让代码更有条理性1、 提取UI组件2、按模块提取业务组件3、按功能提取功能组件二、利用v-bind使组件的属性更具有可读性三
- 如题:我写入关键字到数据库,多的时候用|隔开了,我提取再做相关文章搜索的时候,我怎么提取用|隔开的文字啊,这样我就好用关键字做搜索啊 回复:
- 单例模式(Singleton Pattern) 是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统
- 一、dim的定义TensorFlow对张量的阶、维度、形状有着明确的定义,而在pytorh中对其的定义却模糊不清,仅仅有一个torch.si