我有两个 HTML 文件。一个文件需要将标题发送到第二个文件。
第二个文件需要接收标题和alert()
。
我需要更改什么才能让代码按需要运行?
cookieTitleSend.html
<html>
<head>
<script type="text/javascript">
function setCookie(Title_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var Title_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=Title_name + "=" + Title_value;
}
</script>
</head>
<body onload="setCookie("Title_name",PHP Hello World,1);">
</body>
</html>
cookieTitleReceive.html
<html>
<head>
<script type="text/javascript">
function getCookie(Title_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==Title_name)
{
return unescape(y);
}
}
}
function checkCookie()
{
var Title_name=getCookie("Title_name");
if (Title_name!=null && Title_name!="")
{
alert("Welcome again your Title is
: " + Title_name);
}
else
{
Title_name=prompt("There is no title :","");
}
}
</script>
</head>
<body onload="checkCookie()">
</body>
</html>
最佳答案
您的代码看起来有一些问题,但您可以从正确连接事件处理程序开始。
<body onload="setCookie('Title_name','PHP Hello World',1);">
关于javascript - 如何使用js通过cookie发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10031200/