inject script tag

K

Kris

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?
 
M

Martin Honnen

Kris said:
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;
}
But when alert no 2 is reached it says "SayHello is not defined".

Loading of resources (like image or script files) happens asynchronously
so don't expect the script file to be loaded and executed directly after
the insertion.
If you want to run code depending on the functions in the script file
then you need to use a load event listener with Mozilla and an
onreadystatechange event handler with IE.
 
K

Kris

Loading of resources (like image or script files) happens asynchronously
so don't expect the script file to be loaded and executed directly after
the insertion.
If you want to run code depending on the functions in the script file
then you need to use a load event listener with Mozilla and an
onreadystatechange event handler with IE.

--

Martin Honnen
http://JavaScript.FAQTs.com/- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -

Ok, so once the script file has been injected I need to wait and use
the onreadystatechange event handler in IE to detect if the script
file has been injected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top