0

この機能を備えたフォームに入力して、1つのページにCookieを保存しています:

<script type="text/javascript">
function WriteCookie()
{
   emailValue = escape(document.form.01_email.value) + ";";
   userIDValue = escape(document.form.01_userID.value) + ";";

   document.cookie="email=" + emailValue;
   document.cookie="userID=" + userIDValue;
}
</script>

<form name="form">
 <input type="email" class="form-textbox validate[required, Email]" id="input_10" name="01_email" size="26" value="email@email.com" />
 <input type="hidden" id="simple_spc" name="01_userId" value="1234" />
</form>

ユーザーがフォームを送信すると別のページにリダイレクトされ、このコードで Cookie を取得しますが、Cookie で電子メールとユーザー ID を見つけて、この新しいページの入力の値に挿入する必要があります。

<script type="text/javascript">
    function ReadCookie()
    {
       var allcookies = document.cookie;
       alert("All Cookies : " + allcookies );

       // 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++){
          name = cookiearray[i].split('=')[1];
          value = cookiearray[i].split('=')[2];
          alert("Email is : " + name + " and UserID is : " + value);
       }
    }
    </script>


    <form name="form">
     <input type="email" class="form-textbox" id="input_10" name="02_email" size="26" value="" />
     <input type="hidden" id="simple_spc" name="02_userId" value="" />
    </form>

ユーザーが複数の Cookie を持っている可能性が高いことはわかっているため、問題が発生している電子メールとユーザー ID のみを見つけます。

4

3 に答える 3

1

これを試して、Cookie を読み取ってください。

function ReadCookie(){
    var key, value, i;
    var cookieArray  = document.cookie.split(';');

    for (i = 0; i < cookieArray.length; i++){
        key = cookieArray[i].substr(0, cookieArray[i].indexOf("="));
        value = cookieArray[i].substr(cookieArray[i].indexOf("=")+1);

        if (key == 'email'){
            alert('Email is ' + value);
        }

        if (key == 'userID'){
            alert('userID is ' + value);
        }
    }
}
于 2012-04-25T17:49:12.747 に答える
0

これを試して:

// Now take key value pair out of this array
for(var i=0; i<cookiearray.length; i++){
    name = cookiearray[i].split('=')[1];
    value = cookiearray[i].split('=')[2];
    if (name == "email") 
      alert("Email is : " + value);
    if (name == "userID") 
      alert("UserID is : " + value);
}
于 2012-04-25T17:43:51.340 に答える
0

私はこの答えを信用しませんが、「なぜそれが機能するのか」が組み込まれたソリューションの優雅さのために、quirksmode.org の PPK よりも優れたものはありません。

コードは次のとおりです。

function createCookie(name,value,days) {
  if (days) {
    var date = new Date(), 
        expires = "";
    date.setTime(date.getTime()+(days*24*60*60*1000));
    expires = "; expires=" + date.toGMTString();
  } else {
    document.cookie = name+"=" + value + expires + "; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=", 
      ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') {
      c = c.substring(1,c.length);
    }
    if (c.indexOf(nameEQ) == 0) {
      return c.substring(nameEQ.length,c.length);
    }
  }
  return null;
}

function eraseCookie(name) {
   createCookie(name,"",-1);
}

これがCookieのPPKです

HTH

于 2012-04-25T18:08:08.130 に答える