This link has been bookmarked by 8 people . It was first bookmarked on 17 Sep 2012, by someone privately.
-
29 Aug 14
-
Javascript files can be included and executed on the fly -- either when loading the page or in run-time
-
This means that HTTP requests are made without the use of XMLHttpRequest or iframes. This post provides a trail of my experiments with the inclusion of external javascript files.
-
Also I wanted to figure out an elegant solution whereby you call only one JS and leave it to that script to include more scripts if needed.
-
The function to include a script looks like this:
-
Well, document.write() is not a preferred method for altering an HTML page.
-
Since the page is an XML tree to begin with, we should be using the DOM methods to include a javascript on the fly
-
Note that in Firefox and IE you can get around without actually having a <head> node to add the script node to, but Opera is more strict and will give you an error.
-
I've read somewhere that (wildly guessing here), more than 50% of the browsers' source code deals with not properly nested tags, unclosed tags, and so on and so on programmer's mistakes made out of pure laziness)
-
The next test waits for the page to load completely before adding a script child to the HTML node
-
Now a bit more of a real life example - the javascript is included when the user performs an action (click).
-
When you
document.writeafter the page is loaded, you basically write to a new page and this is not what the user expects. -
To accomplish this effect I also coded a Javascript equivalent of the PHP's in_array().
-
include_once
-
Using this technique you can have only one
<script src="..." ...>tag in your HTML source -
Then I thought about this for a while and I realized that including new scripts on the fly is in essence another way of making a remote call, yes, the ever so popular remote scripting
-
In addition to the two techniques used for making remote calls - XMLHttpRequest (or AJAX) and using iframes, with this javascript DOM inclusion described above you can also make HTTP requests without reloading the page.
-
Like I said I don't see that as being a full-scale alternative to XMLHttpRequest, because it makes GET requests only and also it always returns javascript.
-
The cross-browser way to tell when a script is loaded is here.
-
-
03 Sep 08
-
12 Nov 07
-
01 Nov 07
-
27 Jul 06
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.