2015-05-06 73 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");来清除该变量。