I load the same script in my page many times. I have some trouble on decide which is loaded first/after in my website, due to the async/load functions.
So, I'd like to put a global variable that count, when the script is loaded, the order of them.
So myScript.js will start with :
(function () {
var privateNumberScriptLoaded;
if (numberScriptLoaded === undefined) {
numberScriptLoaded = 0;
}
else {
numberScriptLoaded = numberScriptLoaded + 1;
}
privateNumberScriptLoaded = numberScriptLoaded;
console.log(privateNumberScriptLoaded);
})();
but when I load it with :
<script src=".js?type=normal" type="text/javascript"></script>
<script src=".js?type=rotation" type="text/javascript"></script>
I get (for two times) numberScriptLoaded is not defined
.
How can I resolve this trouble? In fact I'll "create" a global variable in my website if it doesnt exist. Than increment it and store in a "private" variable for each script, so I can save the order of the execution for each script.
I load the same script in my page many times. I have some trouble on decide which is loaded first/after in my website, due to the async/load functions.
So, I'd like to put a global variable that count, when the script is loaded, the order of them.
So myScript.js will start with :
(function () {
var privateNumberScriptLoaded;
if (numberScriptLoaded === undefined) {
numberScriptLoaded = 0;
}
else {
numberScriptLoaded = numberScriptLoaded + 1;
}
privateNumberScriptLoaded = numberScriptLoaded;
console.log(privateNumberScriptLoaded);
})();
but when I load it with :
<script src="http://www.mywebsite.com/widget/myScript.js?type=normal" type="text/javascript"></script>
<script src="http://www.mywebsite.com/widget/myScript.js?type=rotation" type="text/javascript"></script>
I get (for two times) numberScriptLoaded is not defined
.
How can I resolve this trouble? In fact I'll "create" a global variable in my website if it doesnt exist. Than increment it and store in a "private" variable for each script, so I can save the order of the execution for each script.
Share Improve this question asked Jun 1, 2012 at 12:45 markzzzmarkzzz 47.9k126 gold badges316 silver badges534 bronze badges 1- possible duplicate of How can I check whether a variable is defined in JavaScript? – Felix Kling Commented Jun 1, 2012 at 13:17
7 Answers
Reset to default 11At present, your script falls prey to The Horror of Implicit Globals. I'd recommend not doing that.
You have three options:
As all global variables end up as properties on
window
, you could usewindow
explicitly:if (!window.numberScriptLoaded) { window.numberScriptLoaded = 1; // 1, not 0 } else { ++window.numberScriptLoaded; }
Unlike the code without the
window.
prefix, that won't throw aReferenceError
, because looking up a property on an object works differently from resolving a freestanding identifier.Live demo | demo page source | source of script it loads
Always put
var numberScriptLoaded;
(with no initializer) at global scope in your script, e.g. outside your scoping function:var numberScriptLoaded; // No initializer (no = 0 or anything)
On the first load, this will create the variable; on subsequent loads, it's a no-op. Then you can do this without a
ReferenceError
:if (!numberScriptLoaded) { numberScriptLoaded = 1; // 1, not 0 } else { ++numberScriptLoaded; }
Live demo | demo page source | source of script it loads
Use
typeof
. If you take thetypeof
a variable that doesn't exist, you don't get aReferenceError
; you get"undefined"
. Then you can create it via thewindow.
prefix (so you're not falling prey to The Horror).if (typeof numberScriptLoaded === "undefined") { // Assigning to window.numberScriptLoaded creates the global window.numberScriptLoaded = 1; // 1, not 0 } else { ++numberScriptLoaded; }
Live demo | demo page source | source of script it loads
You should use typeof
if (typeof numberScriptLoaded === 'undefined') {
Try
if ( 'undefined' === typeof numberScriptLoaded ) {
numberScriptLoaded = 0;
} else {
numberScriptLoaded = numberScriptLoaded + 1;
}
myGlobalVar = typeof myGlobalVar == "undefined"? "New Value" : myGlobalVar;
myGlobalVar = typeof myGlobalVar == "undefined"? "Totally New Value" : myGlobalVar;
alert(myGlobalVar);
http://jsfiddle.net/8gnuwaah/2/
Global variables are direct attributes of window
object. So if you'd like to init global variable from anywhere just type:
window.variableName = "what_ever_you_want"
As a best practice and to prevent this type of errors, all variables should be initialized before being used in your script.
You should put:
var numberScriptLoaded;
Just before your closure and the error won't happen.
If you're going to use the window
to store the global, then you can do it in a single line with:
window.numberScriptLoaded = (window.numberScriptLoaded || 0) + 1