473,513 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem dynamically adding script tag to xhtml document

You can see the program at:
http://dante.acomp.usf.edu/Communica...ator/index.php

I am trying to dynamically add script tags to my webpage, but I get an
error that the first javascript literal object is not defined.

function initialize() {
insertScriptTags("http://dante.acomp.usf.edu/CommunicationSimulator/prototype.js");
insertScriptTags("http://dante.acomp.usf.edu/CommunicationSimulator/scriptaculous.js");
insertScriptTags("http://dante.acomp.usf.edu/CommunicationSimulator/commsim.js");
CommSim.initialize();
}
function insertScriptTags(libraryName) {
var
l=document.createElementNS("http://www.w3.org/1999/xhtml","script");
l.setAttribute("type", "text/javascript");
l.setAttribute("src", libraryName);
document.getElementsByTagName("head")[0].appendChild(l);
}

CommSim is defined in commsim.js, and it does getting added to the
document, when I look, using Firebug.

How do I get it to be usable by my application?

I am trying to see how little I need in my webpage, for demo purposes.
Thanx for any help.

May 19 '06 #1
1 1634


James Black wrote:

I am trying to dynamically add script tags to my webpage, but I get an
error that the first javascript literal object is not defined.

function initialize() {
insertScriptTags("http://dante.acomp.usf.edu/CommunicationSimulator/prototype.js");
insertScriptTags("http://dante.acomp.usf.edu/CommunicationSimulator/scriptaculous.js");
insertScriptTags("http://dante.acomp.usf.edu/CommunicationSimulator/commsim.js");
CommSim.initialize();
}
function insertScriptTags(libraryName) {
var
l=document.createElementNS("http://www.w3.org/1999/xhtml","script");
l.setAttribute("type", "text/javascript");
l.setAttribute("src", libraryName);
document.getElementsByTagName("head")[0].appendChild(l);
}

CommSim is defined in commsim.js, and it does getting added to the
document, when I look, using Firebug.


The problem is simply that loading script documents and parsing and
executing them takes time meaning when your code does the appendChild
the browser starts fetching the script document aysnchronously but your
code continues directly.
Your best bet is to define a callback function and have the loaded
script document call that callback function, that way there aren't any
synchronization problems (at least as long as there is only one script
to load).
Some browsers also support setting up load handlers or readystatechange
handlers on script elements but doing that cross browser is difficult,
Mozilla should support a load event listener, IE/Win an
onreadystatechange event handler, and other browsers might not support
anything.
--

Martin Honnen
http://JavaScript.FAQTs.com/
May 20 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
1792
by: Nick | last post by:
Right now I'm using document.write("<script language='javascript' src='jsFile" + i + ".js'></script>"); It works -- I have a lot of data in each file and only want the visitor to have to...
11
1897
by: Dagwood Bumstead | last post by:
I play around with js a little... I just don't get this. The file below is just trying out some things... it does exactly what I want (hides/displays some things, no big deal) The problem is...
1
2448
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
1
4812
by: avpkills2002 | last post by:
I seem to be getting this weird problem in Internet explorer. I have written a code for parsing a XML file and displaying the output. The code works perfectly fine with ffx(Firefox).However is not...
0
7267
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
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7120
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
7542
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
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.