Why to never do that
You should never declare variables like that, it has been described here
Then What?
On one page, do:
window.globals = {};
window.globals.my_variable = 'ABC';
On the script, add:
var globals = window.globals;
globals.my_variable;//Gets 'ABC'
This will keep all variables safe in a
global
place. Then we can get all global variables at once, increasing speed.
Don't forget to wrap all your code in something like:
(function() {
//Code here
})();
Functions
To make this easier I made functions:
setSharedVar (name, value) {
if (!"globals" in window) {
window.globals = {};
}
window.globals[name] = value;
}
getSharedVar (name) {
if (!"globals" in window) {
window.globals = {};
return null;
} else if (!name in window.globals) {
return null;
} else {
return window.globals[name];
}
}
Examples
Script 1:
setSharedVar('id', 5);
Script 2:
if (getSharedVar('id') === 5) {
alert('Success!');
}
Alerts 'Success!'