javascript - 如何使用js通过cookie发送参数

标签 javascript html cookies

我有两个 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/

相关文章:

javascript - 在日期选择器中将今天设置为该月的开始日

java - Packery由html发起。我如何实现imageLoaded?

javascript - 一个 cookie 上的多个值

iis - 重写 IIS 7 中响应头的规则(替换 cookie 路径)

javascript - meteor 进入关键事件

javascript - 如何将 word 变量附加到我的 div 中?

javascript - 粘性导航栏在滚动时跳转(JS、HTML、CSS)

javascript - 如何在 JQuery 中为 addClass/removeclass 设置动画

html - 如何在 css 中为导航菜单设置 border-left?

javascript - 如何使用 jQuery 从 cookie 中删除特定的 cookie