js存放cookie一般的写法,如:document.cookie="userName=admin";,如果是多个键值对:document.cookie="userName=admin; userPass=123";
下面是js操作cookie保存用户的登录信息:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script language="javascript" type="text/javascript">
- function addCookie(name,value,days,path){ /**添加设置cookie**/
- var name = escape(name);
- var value = escape(value);
- var expires = new Date();
- expires.setTime(expires.getTime() + days * 3600000 * 24);
- //path=/,表示cookie能在整个网站下使用,path=/temp,表示cookie只能在temp目录下使用
- path = path == "" ? "" : ";path=" + path;
- //GMT(Greenwich Mean Time)是格林尼治平时,现在的标准时间,协调世界时是UTC
- //参数days只能是数字型
- var _expires = (typeof days) == "string" ? "" : ";expires=" + expires.toUTCString();
- document.cookie = name + "=" + value + _expires + path;
- }
- function getCookieValue(name){ /**获取cookie的值,根据cookie的键获取值**/
- //用处理字符串的方式查找到key对应value
- var name = escape(name);
- //读cookie属性,这将返回文档的所有cookie