473,397 Members | 2,033 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,397 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 4889
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.