473,386 Members | 1,694 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,386 software developers and data experts.

loading new JavaScript after the web page is loaded

I am using this to load the client JavaScript for a web application
when it is selected by the user) via an Ajax connection to the server.

I have found only two ways of loading new JavaScript after the web page
is loaded.

1. Create a new script element (where head is the id of the head tag):
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src', 'scripts/myscript.js');
s.setAttribute('defer', false);
document.getElementById('head').appendChild(s);

2. eval() of a string sent from the server via XmlHttpRequest.

The first method does not work with Apple Macintosh Safari. There is an
article on the apple support website
http://lists.apple.com/archives/Webc.../msg00024.html

Does anyone know of any other methods that are cross-browser?

I already know about the IFrame workaround, but I have not been able to
determine if that is really portable and practical. Does anybody have
experience with this?

Thanks,

Gerard Vignes
http://www.GerardVignes.com
Seattle, WA

Dec 24 '06 #1
2 1862
www.gerardvignes.com said the following on 12/23/2006 9:55 PM:
I am using this to load the client JavaScript for a web application
when it is selected by the user) via an Ajax connection to the server.

I have found only two ways of loading new JavaScript after the web page
is loaded.

1. Create a new script element (where head is the id of the head tag):
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src', 'scripts/myscript.js');
s.setAttribute('defer', false);
document.getElementById('head').appendChild(s);
That is not the best way to load a script after the page has loaded when
using a .js file. Search the archives for the word "loadJSFile".
2. eval() of a string sent from the server via XmlHttpRequest.

The first method does not work with Apple Macintosh Safari. There is an
article on the apple support website
http://lists.apple.com/archives/Webc.../msg00024.html
Never have seen that article but I did know, before now, that loading a
..js file on the fly in Safari doesn't work (It doesn't support setting
the .text property of a script block either).
Does anyone know of any other methods that are cross-browser?
Safari supports .createTextNode with a script block. If you are
retrieving the .js file via an AJAX request, then you can read the
contents and use createTextNode to inject it into the page. Check the
archives for a post entitled "createTextNode and IE7"

<URL:
http://groups-beta.google.com/group/comp.lang.javascript/browse_thread/thread/7e23f42490c301de/54a4c31200e06b0d?lnk=gst&q=createTextNode+IE&rnum= 1#54a4c31200e06b0d>

And you can read about the different ways to inject scripts, what
browsers support it, and what browsers don't support the different methods.
I already know about the IFrame workaround, but I have not been able to
determine if that is really portable and practical. Does anybody have
experience with this?
IFrame was the pre-AJAX way of doing it. It is very widely supported.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 24 '06 #2
Thanks Randy!

I appreciate your response. I am looking at your solutions. I'm glad
there are other options.

Gerard Vignes
http://www.GerardVignes.com
Seattle, WA

Dec 24 '06 #3

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

Similar topics

2
by: Richard Bell | last post by:
Newbie question ... please respond to group I'm trying to open/navigate to a url. How can I tell when the page is fully loaded? Testing suggest that window.open returns before the page is...
3
by: Roy Wang | last post by:
hi, My problem is how to determining when the XML file has loaded using javascript. I loaded an xml file using javascript in a web page. The code below is loading the xml file for IE:...
4
by: Adrian MacNair | last post by:
Hi, I created an image gallery which displays 63 images in a slideshow. The problem is that the show was slow because each image loaded one at a time during the show. No problem right? I just...
17
by: CES | last post by:
All, I was wondering if their is a way of loading an external script fill from within a script?? <script language="javascript" type="text/javascript"> function test(var){ <script...
2
by: Jean Pierre Daviau | last post by:
When I have something like this in a js file. listeImg = new Array(); listeImg = new Image(w,h); listeImg.src = "centre1.jpg"; etc Are the images loaded after the page is loaded? In the same...
5
by: Tudor Tihan | last post by:
Hi, This is my first post here, so please be kind. I have tryed to make a javascript html page loader by using an invisible <IFrame> and some javascript variable text passing between...
1
by: ozzy.osborn | last post by:
Hello All, I have been struggling with a cross browser solution to loading external javascript files on the fly. I have been successful using the following code in IE6: var newScr =...
6
by: Venkatesh | last post by:
Hello All, I have couple of doubts regarding the concept of on-demand javascript loading using javascript code. I could see on the net different techniques for achieving this - techniques like:...
6
by: ernesto.tejeda | last post by:
Hello, I have a couple of questions regarding the loading of .js files for the browser and would like anyone to point me wher to find the answer (or if you know the answer and tell me will do just...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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?
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
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
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...

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.