I have my web applicatoin initialize method run based upon the page that is loaded.
I need global variables available to only page 1 of 2. So what I want to do is define and initializ a global variable only if page 1 is loaded. I’m sure there is a way to do this but I do not know how to.
I pasted my releveant intitialze method and the two global I need. Currently they are defined and initialized on both page 1 reload and page 2 reload.
I want them only defined an intialized for page 1 reload.
/********************
group:globals
********************/
var local =
{
client_validation:1,
persistent_element:'hide_1'
};
window.onload=initialize_page;
/*
- initialize_page() - function activated on each page load or reload. Set by global window.onload
*/
function initialize_page()
{
var signin_found;
/*Used to determine which page is loaded / reloaded*/
if(signin_found=document.getElementById('signin_button'))
{
signin_found.onclick=interface_signin;
/*Interfaces and bindings*/
I think you’re probably overcomplicating this for yourself given that the variables in question are using negligable memory, but anyway the simplest way I can think of is to do this:
Note that you can’t declare a variable conditionally by putting a
varstatement inside anif: JavaScript doesn’t have block scope (only function scope) so it will “hoist” the declaration up outside of theif. But in the case of global variables you can get around this because globals are essentially just properties of thewindowobject so you can create the property conditionally as shown above.If you later realise that both pages have certain variables in common but also have their own separate requirements you can do this: