Problem with getting data out of injected script
template.html contains the following html and is located at
http://example.com/template.html:
<html>
<head>
<title></title>
</head>
<body>
<script src="http://eteneo.dk/synd.js" type="text/javascript"></
script>
</body>
</html>
http://eteneo.dk/synd.js contains the following:
function $(id){
return document.getElementById(id);
}
function $$(tagName){
return document.getElementsByTagName(tagName);
}
var eseHeadTag = $$("head").item(0);
var eseScriptTag = document.createElement("script");
eseScriptTag.setAttribute("src", "http://eteneo.dk/sample.js");
eseScriptTag.setAttribute("type", "text/javascript");
eseScriptTag.setAttribute("id", "eseScriptTagId");
eseHeadTag.insertBefore(eseScriptTag, eseHeadTag.firstChild);
alert($("eseScriptTagId").src);
alert(SayHello("some one"));
And the script file that is injected called sample.js contains the
following:
function SayHello(whatsMyNameAgain){
return "Hello "+ whatsMyNameAgain;
}
Now, when I open http://example.com/template.html alert no 1 is
showing telling me the scr attribute of element with id
"eseScriptTagId" has "http://eteneo.dk/sample.js" as value. Which
tells me the script is injected into the HTML.
But when alert no 2 is reached it says "SayHello is not defined".
As far as I can see there is no significant difference between the
code above and this example:http://www.xml.com/pub/a/2005/12/21/json-
dynamic-script-tag.html?page=1
Any idea why I cant inject the .js script and call the SayHello
function inside it?