Reputation: 49
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).
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.
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.
Upvotes: 0
Views: 56
Reputation: 8660
you could use sessionStorage
to store a variable if logged in:
sessionStorage.setItem("loggedInStatus", "In");
then upon page load you could use an if statement and if it returns true use the $.getScript()
method:
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.
Upvotes: 1