I am trying to set up a global variable that will be read across all pages so the page knows one is logged in. Once one logs in they are re-directed to a /myaccount.asp page. I have put the following code on there:
var $loggedIn = 1;
Now on ALL my pages I have the following code:
var $loggedIn;
$(document).ready(function () {
if ($loggedIn == 1) {
alert("You are logged In!")
} else {
alert("Not Logged In Yet!")
}});
Of course the above ONLY works on the /myaccount.asp page, otherwise it shows the alert “Not Logged In Yet!” on other pages. Is there a way to set a variable so it reads globally, I’m doing something simply wrong?
Javascript variables do not stay constant across page refreshes. As soon as you move on to a new page or refresh the page you’re on, everything in Javascript gets refreshed and wiped out. You will need to assign your value as a query parameter (GET) in your URL, or better yet, look into creating cookies.