I need to use a value defined on a webpage in an external javascript file.
<html>
<body>
<script src="myscript.js"></script>
<Script Language="JavaScript">
var theVar = 'theValue';
</Script>
</body>
</html>
I need to be able to use theVar in the external javascript file called "myscript.js".
The challenge is that theVar is defined after the external script is called - the order of the calls must not be changed.
Therefore within "myscript" I have defined a function that loops until theVar has been defined in the html document.
See below for the code I have tried to use to achieve this from "myscript.js".
Should it work? If not can you point out where I may be going wrong?
====================================
function TestVariable(){
if(typeof theID != "undefined") {
break;
}
else{
setTimeout("TestVariable();", 300);
}
}
TestVariable();
=================
In addition, if it is possible, I would like to use the value of the captured variable to construct a dynamic url within the external file.
Ideas leading to a solution very much appreciated.
cheers
<html>
<body>
<script src="myscript.js"></script>
<Script Language="JavaScript">
var theVar = 'theValue';
</Script>
</body>
</html>
I need to be able to use theVar in the external javascript file called "myscript.js".
The challenge is that theVar is defined after the external script is called - the order of the calls must not be changed.
Therefore within "myscript" I have defined a function that loops until theVar has been defined in the html document.
See below for the code I have tried to use to achieve this from "myscript.js".
Should it work? If not can you point out where I may be going wrong?
====================================
function TestVariable(){
if(typeof theID != "undefined") {
break;
}
else{
setTimeout("TestVariable();", 300);
}
}
TestVariable();
=================
In addition, if it is possible, I would like to use the value of the captured variable to construct a dynamic url within the external file.
Ideas leading to a solution very much appreciated.
cheers