2016-04-14 10 views
0

JavaScriptのCookieに関する質問があります。私は、このサイトからJavaScriptクッキーを作成して読んでいる例を使っています:http://www.tutorialspoint.com/javascript/javascript_cookies.htm。クッキーは同じページに読み込んだり書き込んだりしますが、一度同じような形式の別のページに行くと、クッキー内の情報はなくなります。JavaScriptクッキーが1ページでのみ動作するのはなぜですか?

私はもともとこれを自分のデスクトップで動作させていましたが、私がテストしているDEVサイトに一度追加してしまえば、それは1ページでしか動作しません。基本的に私はフォームを使って1つのページにクッキーを設定することができますし、別のページには読み込むクッキーはありません。しかし、私は2番目のフォームに別のクッキーを作成することができます。 1ページ目のフォームに戻ると、作成した最初のCookieがフォームフィールドに入力されます。

ので:ウェブサイトに関する

Form 1 page - cookie 1 created 
- then go to - 
Form 2 page - cookie 1 doesn't exist but I can create cookie 2 
- then go to - 
Form 1 page - cookie 1 loads into form 1 
- then go to - 
Form 2 page - cookie 2 loads into form 2 

追加情報:

Apacheサーバ PHP 5.4 AngularJS 1.2.26 Webサービス 他のJavaScriptとjQueryファイル サードパーティのスクリプト

について私がdocument.cookieで見るのは、それをデバッグするときにphpsessidだけです。これにより、他のページのフォーム上にCookieが掲載されなくなる可能性がありますか?これらのフォームは、DEVのウェブサイトと同じである

デスクトップ版と同じドメイン上のすべてなので...:

ページ1

<html> 
    <head> 

     <script src="tutorialspoint-cookies.js" type="text/javascript"></script> 

    </head> 

    <body> 

     <h1>FORM 1</h1> 
     <form name="form_000c" id="form_000c" action=""> 
     <label>First Name:</label> 
     <input type="text" name="First_Name" id="First_Name" /><br /> 
     <label>Last Name:</label> 
     <input type="text" name="Last_Name" id="Last_Name" /><br /> 
     <label>Email:</label> 
     <input type="text" name="Email" id="Email" /><br /> 
     <label>Phone Number:</label> 
     <input type="text" name="Phone" id="Phone" /><br /> 
     <label>Timeline:</label> 
     <select name="Timeline" id="Timeline"> 
      <option value="time1">Timeline 1</option> 
      <option value="time2">Timeline 2</option> 
      <option value="time3">Timeline 3</option> 
      <option value="time4">Timeline 4</option> 
     </select><br /> 
     <label>Measurements:</label> 
     <select name="Measurements" id="Measurements"> 
      <option value="meas1">Measurement 1</option> 
      <option value="meas2">Measurement 2</option> 
      <option value="meas3">Measurement 3</option> 
      <option value="meas4">Measurement 4</option> 
     </select><br /> 
     <input type="button" value="Set Cookie" onclick="WriteCookie();"/> 
     </form> 
     <a href="tutorialspoint-cookies-2.html">go to page 2</a> 

    </body> 
</html> 

2ページ

<html> 
    <head> 

     <script src="tutorialspoint-cookies.js" type="text/javascript"></script> 

    </head> 
    <body onLoad="ReadCookie()"> 

     <h1>FORM 2</h1> 
     <form name="form_000c" id="form_000c" action=""> 
     <label>First Name:</label> 
     <input type="text" name="First_Name" id="First_Name" /><br /> 
     <label>Last Name:</label> 
     <input type="text" name="Last_Name" id="Last_Name" /><br /> 
     <label>Email:</label> 
     <input type="text" name="Email" id="Email" /><br /> 
     <label>Phone Number:</label> 
     <input type="text" name="Phone" id="Phone" /><br /> 
     <label>Timeline:</label> 
     <select name="Timeline" id="Timeline"> 
      <option value="time1">Timeline 1</option> 
      <option value="time2">Timeline 2</option> 
      <option value="time3">Timeline 3</option> 
      <option value="time4">Timeline 4</option> 
     </select><br /> 
     <label>Measurements:</label> 
     <select name="Measurements" id="Measurements"> 
      <option value="meas1">Measurement 1</option> 
      <option value="meas2">Measurement 2</option> 
      <option value="meas3">Measurement 3</option> 
      <option value="meas4">Measurement 4</option> 
     </select><br /> 
     <input type="button" value="Set Cookie" onclick="WriteCookie();"/> 
     </form> 
     <a href="tutorialspoint-cookies.html">go to page 1</a> 

    </body> 
</html> 

JavaScriptクッキー

<!--http://www.tutorialspoint.com/javascript/javascript_cookies.htm 
    function WriteCookie(){ 
     cookievalue1 = escape(document.form_000c.First_Name.value) + ";"; 
     cookievalue2 = escape(document.form_000c.Last_Name.value) + ";"; 
     cookievalue3 = escape(document.form_000c.Email.value) + ";"; 
     cookievalue4 = escape(document.form_000c.Phone.value) + ";"; 
     cookievalue5 = escape(document.form_000c.Timeline.value) + ";"; 
     cookievalue6 = escape(document.form_000c.Measurements.value) + ";"; 
     document.cookie = "First_Name=" + cookievalue1; 
     document.cookie = "Last_Name=" + cookievalue2; 
     document.cookie = "Email=" + cookievalue3; 
     document.cookie = "Phone=" + cookievalue4; 
     document.cookie = "Timeline=" + cookievalue5; 
     document.cookie = "Measurements=" + cookievalue6; 
     alert("Setting Cookies : " + "First_Name=" + cookievalue1 + "Last_Name=" + cookievalue2 + "Email=" + cookievalue3 + "Phone=" + cookievalue4 + "Timeline=" + cookievalue5 + "Measurements=" + cookievalue6); 
    } 

    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++){ 
      name = cookiearray[i].split('=')[0]; 

      // the cookie is leaving a white space in the name so we need to remove it with .trim() 
      name = name.trim(); 
      value = cookiearray[i].split('=')[1]; 
      document.getElementById(name).value = value; 
     } 
    } 

答えて

0

クッキーを設定するときは、パスも指定する必要があることを覚えておくことが重要です。 JavaScriptの

document.cookie = "First_Name=" + cookievalue1 + " path=/"; 
document.cookie = "Last_Name=" + cookievalue2 + " path=/"; 
document.cookie = "Email=" + cookievalue3 + " path=/"; 
document.cookie = "Phone=" + cookievalue4 + " path=/"; 
document.cookie = "Timeline=" + cookievalue5 + " path=/"; 
document.cookie = "Measurements=" + cookievalue6 + " path=/"; 
+0

おかげでクッキーを設定しながら

// パス=/を使用しています。これは現在すべてのページで機能しています。他のクッキーがスクリプトを失敗させないように、最後の行を変更しました。 –

+0

if(name == "First_Name" || name == "Last_Name" || name == "Email" || name == "Phone" || name == "Timeline" || name == "Measurements") { \t \t \t document.getElementById(name).value = value; \t \t} –

関連する問題