在.html中用js将文本域中数据通过按钮存入cookie 并在这个.html中的<p></P>里输出cookie的值,应该怎么写

如题所述

Cookie基本操作方法借用arthur24的写法,html代码如下:

<!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>
<title> new document </title>
<script language="javascript">
function setCookie(name,value,path) //两个参数,一个是cookie的名子,一个是值
{
//var Days = 30; //此 cookie 将被保存 30 天
//var exp = new Date(); //new Date("December 31, 9998");
//exp.setTime(exp.getTime() + Days*24*60*60*1000);
//document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
//document.cookie = name + "="+ escape (value) + ";expires=";
document.cookie = name + "="+ escape (value) + ";path="+path+";expires=";
}
function getCookie(name) //取cookies函数
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
try{
if(arr != null) return decodeURI(arr[2]); return null;
}catch(err){

}

}
function delCookie(name) //删除cookie
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

function showCookie(){
var o=document.getElementById("cookie_value");
var v=getCookie('test_cookie');
if(v != null) o.innerHTML='Cookie值:' + v;
}

function doSetCookie(){
var o=document.getElementById("test_value");
var ov=o.value;
setCookie('test_cookie',ov,'');
showCookie();
}
</script>
</head>

<body onload="showCookie();">
<p id="cookie_value"></p>
<form>
<textarea id="test_value" rows="10" cols="50"></textarea>
<br />
<input value="设置" type="button" onclick="doSetCookie();" />
</form>
</body>
</html>追问

那想用按另一个按钮在文本域内显示以存储的cookie,应该怎么写?
像调用超级链接的地址为cookie值呢?

追答

唉,算了吧!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-22
js:
//cookie操作
function setCookie(name,value,path) //两个参数,一个是cookie的名子,一个是值
{
//var Days = 30; //此 cookie 将被保存 30 天
//var exp = new Date(); //new Date("December 31, 9998");
//exp.setTime(exp.getTime() + Days*24*60*60*1000);
//document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
//document.cookie = name + "="+ escape (value) + ";expires=";
document.cookie = name + "="+ escape (value) + ";path="+path+";expires=";
}
function getCookie(name) //取cookies函数
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
try{
if(arr != null) return decodeURI(arr[2]); return null;
}catch(err){

}

}
function delCookie(name) //删除cookie
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}追问

在body中怎么调用
在《form》里

本回答被提问者采纳
相似回答