商店的javascript cookie,检索并附加到网址

时间:2015-03-28 09:50:08

标签: javascript html cookies

我有两个html页面

第1页有这行HTML代码

    <li><input type="button" name="userid" id="userid" type="text" onclick="uid()" value="Type Your ID"/></li>

第2页有......

<a href="http://example.com/?id=sara">Profile</a><br/> <a href="http://example.com/art/?id=sara">art</a><br/> <a href="http://example.com/acc/?id=sara">account</a><br/> <a href="http://app.example.com/?id=sara">link</a><br/>

如何使用cookie存储和获取用户ID并将其附加到一个有效期为一个月的href标记?

希望你的极客提供一个有效的例子。

2 个答案:

答案 0 :(得分:0)

试试这个会帮到你

Page1.html

<html>
<head>
<script type="text/javascript">
<!--
function uid()
{
   if( document.getElementById("userid").value == "" ){
      alert("Enter some value!");
      return;
 }

  var now = new Date();
  now.setMonth( now.getMonth() + 1 ); 
  document.cookie = "expires=" + now.toUTCString() + ";"
  cookievalue= escape(document.getElementById("userid").value) + ";";
  document.cookie="id=" + cookievalue;
  document.cookie = "expires=" + now.toUTCString() + ";"
  document.cookie="id=" + cookievalue;


  alert("Setting Cookies : " + "id=" + cookievalue );
}
//-->
</script>
</head>

<body>

    <form action="Page2.html">
    <input type="text" name="userid" id="userid" type="text" />
    <input type="submit" onclick="uid()" value="Store Cookie and proceed to next pg">
    </form>
</body>
</html>

Page2.html

<html>
<head>
<script type="text/javascript">
<!--
var value;
var id;
function ReadCookie()
{
   var allcookies = document.cookie;

   // Get all the cookies pairs in an array
   cookiearray  = allcookies.split(';');

   // Now take key value pair out of this array
   for(var i=0; i<cookiearray.length; i++){
      id = cookiearray[i].split('=')[0];
      value = cookiearray[i].split('=')[1];
      if(id.trim()==="id"){
      document.getElementById('wel').innerHTML="welcome "+value;
      alert("Key is : " + id + " and Value is : " + value);
      break;
      }  
 }
}

function app(u)
{
  alert(u+value);
location.href = u+value;
//window.location.replace(u+value);
return false;    
}

//-->
</script>
</head>

<body onload="ReadCookie()">

<p id="wel"></P>
<a href="Profile.html?id=" onclick="app(this.href);return false;"> Profile</a><br/>
 <a href="art.html?id=" onclick="app(this.href);return false;">art</a><br/>
 <a href="account.html?id=" onclick="app(this.href);return false;">account</a><br/>
 <a href="link.html?id=" onclick="app(this.href);return false;">link</a><br/>
</body>

</html>

答案 1 :(得分:0)

我太忙了,但我认为这可能会有所帮助

阅读本文Ref

我发现了; HTML本地存储提供了两个用于在客户端上存储数据的对象:

window.localStorage - 存储没有过期日期的数据 window.sessionStorage - 存储一个会话的数据(选项卡关闭时数据丢失)

所以我想出了使用Php来完成解决方案的想法。

<scripts>
function SaveId(id){
$.ajax({
url: "side.php?newId="+id,
success: function (data) {
//do some thing you could append the data to an element attribute
}//sucess
});

}//Save Id function close here

$("body").find("li").click(function (){
var newId=$(this).attr("data-user-id");
SaveId(newId);//fire the SaveId()
});
</scripts>

==服务器端== side.php

<?php
 if(isset($_REQUEST['newId'])){
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;//Locahost helper

$newQ=$_REQUEST['newId'];
if (isset($_COOKIE['userId'])){
echo $_COOKIE['userId'];//return store user Id we already have one 
}
else
{
setcookie('userId', $newQ, time()+60*60*24*30, '/', $domain, false);//1 Month you said
echo $_REQUEST['newId'];//As it is not available in storage until page reload
}
}//isest ends here
?>

在第一页

<?php 
//after login get user id from somewhere
$userId=$_REQUEST['userId'];
?>

<li data-user-id=<?php echo $userId; ?> id= > I am first List </li>

在下一页

<?php $saraId=$_COOKIE['userId'];?>

<a href="whereToLink/?id=<?php echo $saraId ?>" >LinkOn</a>