469,330 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

inject script tag

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?

Apr 26 '07 #1
2 4686
Kris wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 26 '07 #2
On 26 Apr., 14:01, Martin Honnen <mahotr...@yahoo.dewrote:
Kris wrote:
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.

--

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.

Apr 26 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by andysideas | last post: by
5 posts views Thread by Nim | last post: by
3 posts views Thread by Angel | last post: by
1 post views Thread by sklett | last post: by
2 posts views Thread by ChrisA | last post: by
1 post views Thread by =?Utf-8?B?SkhpbGJDUk0=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.