I am working on the development of a website with login, profile view functionality. So I was working with the login and getting to the profile part. I deployed the login part correctly. I get back an array of data to fill in the profile.html page. However, when I go to profile.html page using location.href, I seem to lost that data as login.js has been reloaded. I have tried with keeping all the jQuery code (login and profile) inside login.js however, it does not work as when I hit the location.href function, whole JS file reloaded. Everyone of my scenario/solution kind of stopped whenever I hit that location.href function. I checked the validity of the data using console.log as well as printing it out in the screen in a div (in the login page).
我正在开发一个具有登录,配置文件视图功能的网站。所以我正在使用登录并进入配置文件部分。我正确部署了登录部分。我收回了一系列数据以填写profile.html页面。但是,当我使用location.href访问profile.html页面时,我似乎丢失了这些数据,因为login.js已经重新加载。我已经尝试将所有jQuery代码(登录和配置文件)保留在login.js中,但是,当我点击location.href函数时,它不起作用,整个JS文件重新加载。每当我点击location.href函数时,我的场景/解决方案的每个人都会停止。我使用console.log检查了数据的有效性,并在div的屏幕上打印出来(在登录页面中)。
My question is how to load the profile.html page without losing my data in the array? I am providing the code for login.js although I dont think that would be necessary.
我的问题是如何加载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);
}
});
}
Thanks in advance.
提前致谢。
1 个解决方案
#1
you could use sessionStorage
to store a variable if logged in:
如果登录,您可以使用sessionStorage存储变量:
sessionStorage.setItem("loggedInStatus", "In");
then upon page load you could use an if statement and if it returns true use the $.getScript()
method:
然后在页面加载时你可以使用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
}
Edit: If you do it this way you would want to make sure to clear out the variable by using sessionStorage.removeItem("loggedInStatus");
on logout.
编辑:如果你这样做,你会希望确保通过使用sessionStorage.removeItem(“loggedInStatus”)清除变量;注销。
#1
you could use sessionStorage
to store a variable if logged in:
如果登录,您可以使用sessionStorage存储变量:
sessionStorage.setItem("loggedInStatus", "In");
then upon page load you could use an if statement and if it returns true use the $.getScript()
method:
然后在页面加载时你可以使用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
}
Edit: If you do it this way you would want to make sure to clear out the variable by using sessionStorage.removeItem("loggedInStatus");
on logout.
编辑:如果你这样做,你会希望确保通过使用sessionStorage.removeItem(“loggedInStatus”)清除变量;注销。