javascript 写入和读取cookie
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 写入和读取cookie相关的知识,希望对你有一定的参考价值。
学习javascript,写了一段测试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>Example 15.1 Cookie示例</title>
</head>
<body>
<h1>
<script type="text/javascript">
function getCookie(name)
var cookies=document.cookie.split(";");
for(var i=0;i<cookies.length;i++)
var s=cookies[i].split("=");
if(s[0]==name) return s[1];
function setCookie(name,value)
document.cookie = name+"="+value;
var lastPerson = getCookie("$name$")||"";
var name = prompt("What's your name?",lastPerson);
var times = getCookie(name)||0;
setCookie(name,times-0+1);
setCookie("$name$",name);
if(times>0)
document.write("Hello "+name+", nice to meet you again!");
else
document.write("Hello "+name+"!");
</script>
</h1>
</body>
</html>
用jquery插件 jquery cookie 操作就很简单方便。
使用的时候,应该现在jquery.js 然后在加载那个jquery cookie
网上自己找找吧 参考技术B 注意分隔后的格:
使用s[0] = s[0].replace(/^\s*/,"").replace(/\s*$/,"");去掉
取值和取键名是不一样的
<!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>Example 15.1 Cookie示例</title>
</head>
<body>
<h1>
<script type="text/javascript">
function getCookie(name)
var cookies=document.cookie.split(";");
for(var i=0;i<cookies.length;i++)
var s=cookies[i].split("=");
s[0] = s[0].replace(/^\s*/,"").replace(/\s*$/,"");
if(s[0]==name) return s[1];
return '';
function setCookie(name,value)
document.cookie = name+"="+value;
//debugger;
var lastPerson = getCookie("cname");
var rawname = lastPerson.split("_");
var name = prompt("What's your name?",rawname[0]);
//var name = 'as';
if (lastPerson!='')
var times = Number(rawname[1]||0)+1;
if(name==rawname[0])
setCookie("cname",rawname[0]+'_'+times)
else
setCookie("cname",name+'_1');
else
setCookie("cname",name+'_1');
if(times>0&&name==rawname[0])
document.write("Hello "+rawname[0]+", nice to meet you again!");
document.write("您是第"+times+"次光临!");
else
document.write("Hello "+name+"!");
</script>
</h1>
</body>
</html>追问
感谢你的热心回答,但是用了你的代码结果还是一样,好像根本没有写进cookie,是不是和浏览器cookie设置有关呢
追答我在IE8和Firefox3.6下测试正常,可以记住上次的名字和访问次数
本回答被提问者采纳 参考技术C 你想要什么效果!Javascript(JS)对Cookie的读取删除写入操作帮助方法
1 var CookieUtils = { 2 get: function (name) { 3 var cookieName = encodeURIComponent(name) + ‘=‘, 4 cookieStart = document.cookie.indexOf(cookieName), 5 cookieValue = null; 6 if (cookieStart > -1) { 7 var cookieEnd = document.cookie.indexOf(‘;‘, cookieStart); 8 if (cookieEnd == -1) { 9 cookieEnd = document.cookie.length; 10 } 11 cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd)); 12 } 13 return cookieValue; 14 }, 15 set: function (name, value, expires, path, domain, secure) { 16 var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value); 17 if (expires instanceof Date) { 18 cookieText += "; expried=" + expires.toGMTString(); 19 } 20 if (path) { 21 cookieText += "; path=" + path; 22 } 23 if (domain) { 24 cookieText += "; domain=" + domain; 25 } 26 if (secure) { 27 cookieText += "; secure"; 28 } 29 document.cookie = cookieText; 30 }, 31 unset: function (name, path, domain, secure) { 32 this.set(name, "", new Date(0), path, domain, secure); 33 } 34 }
这样即可以调用CookieUtils.get/set/unset方法进行Cookie操作。
项目完成后会对本方法进行进一步完善。
参考自《Javascript高级程序设计》
以上是关于javascript 写入和读取cookie的主要内容,如果未能解决你的问题,请参考以下文章
如何在 html 网站上使用 javascript 读取和写入另一个文件?
通过浏览器使用 javascript/jQuery 读取和写入 ID3 标签到本地文件?