473,388 Members | 873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 4888
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tvmaly | last post by:
Due to the restrictions I have at my host, I cannot use smtplib in my email cgi script. They gave me a script they use that calls nullmailer-inject. I am trying to figure out how to add the...
2
by: andysideas | last post by:
I am an administrator of a large organisation. We require the ability to be able to inject code into all pages that people view. There are a number of requirements. I am hoping there might be a...
5
by: Nim | last post by:
HI, I would like to know if it's possible inject a dll (create with c++) into an application with c# like c++, and which instruction use c# for do this
3
by: Angel | last post by:
Hi, I have a page which shows a matrix of names and grades for given period. All the grades fields must be editable and they can be up to 200 fields (20 students with 10 grades each) Since...
1
by: sklett | last post by:
I want to implement a simple form element focus script that will change the color of a form element when it gets focus and revert back when it loses focus. I've got the code working (simple) but...
3
by: inanis | last post by:
Salutations! I have an odd situation and know nothing about regular expressions, but I believe I will need to use them to attempt what I have to do. I am running a script in PHP 5 that takes...
2
by: ChrisA | last post by:
Is there a way to inject a large javascript function into an asp.net page, in 1.1 and 2.0?
1
by: =?Utf-8?B?SkhpbGJDUk0=?= | last post by:
I am trying to write a httpmodule to inject javascript includes on some of the aspx pages we have. For instance, I want to inject into the resulting IIS response something like... ...
5
by: jeffos101 | last post by:
Hi, I am trying to do the following and would like to know if it's possible using Javascript/Ajax/and some DB I have an Iframe hosted on one server that loads a page which is located on another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.