2013-12-10 47 views
0

我越來越堅果的東西。JavaScript測試變量設置Cookie

我想要做的是在加載某個頁面時設置一個cookie,以便在另一個頁面上並且已經設置好cookie時刷新頁面。

我的條件測試沒有按照我的預期做出反應。

這裏是我在:

$(window).load(function() {  

     var page=$(".listing_voyage"); 

     //alert('page:'+page.length); 
     //alert('cookie:'+getCookie('refresh')); 

     if (getCookie('refresh') && page.length)  
     {       
      window.location.reload(true); 
      SetCookie("refresh",false); 
      alert('i am reloading'); 
     } 


    }); 



    var page_produit=$("#page_produit"); 
    if (page_produit.length)   
    { 
     SetCookie("refresh",true); 
    } 

回答

1

一旦你重新加載頁面時,JavaScript丟失,一切重新開始

window.location.reload(true); // reloads page 
SetCookie("refresh",false); // and this is not executed, as the page reloaded 
alert('i am reloading'); 

你必須之前設置Cookie 重裝

SetCookie("refresh",false); 
window.location.reload(true); 

但是,爲什麼你reloa在頁面開始之前,這似乎是一件奇怪的事情?