Can't access variables from dynamically loaded javascript
Posted
by Menno
on Stack Overflow
See other posts from Stack Overflow
or by Menno
Published on 2010-04-29T11:42:02Z
Indexed on
2010/04/29
11:47 UTC
Read the original article
Hit count: 197
I'm using a fairly simple system to load javascript dynamically:
include = function (url) {
var e = document.createElement("script");
e.src = url;
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
};
Let's say I have a file test.js which has the following contents:
var foo = 4;
Now, in my original script, I want to use
include(test.js);
console.log(foo);
However, I get a 'foo has not been defined' error on this. I'm guessing it has to do with the dynamic script being included as the last child of the <head>
tag. How can I get this to work?
© Stack Overflow or respective owner