473,767 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.create Element('script ');
s.setAttribute( 'type','text/javascript');
s.setAttribute( 'src', 'scripts/myscript.js');
s.setAttribute( 'defer', false);
document.getEle mentById('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 1891
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.create Element('script ');
s.setAttribute( 'type','text/javascript');
s.setAttribute( 'src', 'scripts/myscript.js');
s.setAttribute( 'defer', false);
document.getEle mentById('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 "createText Node and IE7"

<URL:
http://groups-beta.google.com/group/comp.lang.javas cript/browse_thread/thread/7e23f42490c301d e/54a4c31200e06b0 d?lnk=gst&q=cre ateTextNode+IE& rnum=1#54a4c312 00e06b0d>

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.javas cript 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
8310
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 completely loaded and displayed. Likewise, window.navigate. The code looks something like this: wHandle = window.open( url, name, flags ); .... // how to tell when url is fully loaded and displayed
3
8504
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: this.xmlDoc= new ActiveXObject("Microsoft.DomDocument"); this.xmlDoc.load(url);
4
3747
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 did a preload script. But then the user has to sit for 5 minutes waiting for 63 images to download! My images are about 640x480 and average 100kb. Is this too much for one page to load? Should I load my slideshow into differerent windows? If so,...
17
2782
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 language="javascript" src="../scripts/base.js" type="text/javascript" /> } </script> Obviously this would cause n error but this would give you an idea of what I'm looking to do. I know I can do this with a simple include but...
2
2995
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 time? Before the page is loaded? The browser decision? If the visitors goes, before the procedure is over, to another page having a link to the same js file, will the other page allows for the rest of the
5
2209
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 entities. This seems to brake the CSS after some depth, or perhaps I am not understanding something.
1
3811
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 = document.createElement("SCRIPT"); newScr.src = "newScr.js"; newScr.type="text/javascript";
6
2776
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: 1. document.write("<script src= language='JavaScript'></script>); 2. sc = document.createElement("<script>"); sc.setAttribute("src", ); and append this to the head
6
2224
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 fine ;) ) - If I have several pages all using 'somejs.js' file this file is shared on disk and is downloaded only once... but how about in memory, is it also shared and just loaded once? -Also, when are the .js files unloaded from memory?
0
9571
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
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
10009
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...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8835
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...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
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 we have to send another system

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.