2015-05-06 74 views
0

我正在開發具有登錄,配置文件視圖功能的網站。所以我正在使用登錄並進入配置文件部分。 我正確部署了登錄部分。我找回一組數據以填充profile.html頁面。但是,當我使用location.href去profile.html頁面時,由於login.js已被重新加載,我似乎丟失了該數據。 我已經嘗試保留login.js中的所有jQuery代碼(登錄和配置文件),但它不起作用,因爲當我點擊location.href函數,整個JS文件重新加載。每當我觸及location.href函數時,我的場景/解決方案的每個人都會停下來。 我使用console.log檢查了數據的有效性,並將它打印在屏幕上的div(在登錄頁面中)。無法從一個jQuery文件獲取數據到另一個

我的問題是如何加載profile.html頁面而不會丟失數組中的數據? 我提供了login.js的代碼,雖然我不認爲這是必要的。

function getData() 
{ 
    $.ajax(
     { 
      url:"http://localhost/dataFetch.php", 
      dataType:"json", 
      type:"GET", 
      success: function(suc) 
      { 
       mainArray = suc; 
       location.href="temp.html"; 
       length = mainArray["desc"].length; 
      }, 
      error: function(err) 
      { 
       alert("connection to server has be interrupted"); 
       console.log(err); 
      } 
     }); 
} 

在此先感謝。

+3

使用'localStorage' – Satpal

+1

^或'sessionStorage' –

回答

1

你可以使用sessionStorage存儲變量如果登錄:

sessionStorage.setItem("loggedInStatus", "In");

然後在網頁加載時,你可以使用一個if語句,如果它返回true使用$.getScript()方法:

var variable = sessionStorage.getItem("loggedInStatus"); 
if (variable != null) { 
    $.getScript("ajax/test.js", function(data, textStatus, jqxhr) { 
     console.log(data); // Data returned 
     console.log(textStatus); // Success 
     console.log(jqxhr.status); // 200 
     console.log("Load was performed."); 
    }); 
} 
else { 
    // do whatever 
} 

編輯:如果以這種方式執行操作,則需要確保在登出時使用sessionStorage.removeItem("loggedInStatus");來清除該變量。