473,772 Members | 2,448 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() {
insertScriptTag s("http://dante.acomp.usf .edu/CommunicationSi mulator/prototype.js");
insertScriptTag s("http://dante.acomp.usf .edu/CommunicationSi mulator/scriptaculous.j s");
insertScriptTag s("http://dante.acomp.usf .edu/CommunicationSi mulator/commsim.js");
CommSim.initial ize();
}
function insertScriptTag s(libraryName) {
var
l=document.crea teElementNS("ht tp://www.w3.org/1999/xhtml","script" );
l.setAttribute( "type", "text/javascript");
l.setAttribute( "src", libraryName);
document.getEle mentsByTagName( "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 1669


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() {
insertScriptTag s("http://dante.acomp.usf .edu/CommunicationSi mulator/prototype.js");
insertScriptTag s("http://dante.acomp.usf .edu/CommunicationSi mulator/scriptaculous.j s");
insertScriptTag s("http://dante.acomp.usf .edu/CommunicationSi mulator/commsim.js");
CommSim.initial ize();
}
function insertScriptTag s(libraryName) {
var
l=document.crea teElementNS("ht tp://www.w3.org/1999/xhtml","script" );
l.setAttribute( "type", "text/javascript");
l.setAttribute( "src", libraryName);
document.getEle mentsByTagName( "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 readystatechang e
handlers on script elements but doing that cross browser is difficult,
Mozilla should support a load event listener, IE/Win an
onreadystatecha nge 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
1850
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 download the necessary file. Is there a better way to do this? Just curious because the page doesn't validate in HTML validators.
11
1925
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 that in the swapcheck(x) function below, if I uncomment the first line (commented with //) the whole thing brings and IE errors the page with an object expected error on line (whichever input control I clicked).
1
2467
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 guy's Javascript "combo box" - http://sandy.mcarthur.org/javascript/select/select.html. It allows my users (when I get some!) to select from a list of preexisting options and also to add a new one by clicking on "add new". Essentially it's a select...
1
4851
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 working in Internet Explorer.(I m using Internet Explorer 6.0). The code is as follows: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.