Email conversation
From | Trevor Munn |
To | Me |
Subject | Incrementally loading script from cache or server |
Date | 5 October 2006 04:10 |
Hello,
Background/Goal - I am trying to develop a mechanism that will allow me to
download jScript "on demand"/as required
Currently I am developing a website using a html and a lot of javascript,
due to the amount I am adding all new scripts to an external file which is
downloaded upon page load and added to the browser cache. This currently
takes a long time even from cache.
The new idea was to break my script file into smaller script files and
include them on the page when required, thus reducing the page load time
considerably. To do this I have tried creating what I call "hollow"
functions which esentially download the selected .js file and overwrite
themselves and all other hollow versions of functions within that particular
js file in the DOM and then run the new version.
I have been having trouble trying to figure out how to perform the
overwrite. And instead I have been adding the script to the head instead.
For example I would have a page containing:
<script type="text/javascript">
var functionCall = "";
var lookupURL = 'http://[IP]/';
function add(val1, val2){
alert("Hollow Add");
document.functionCall = "add(" + val1 + ", " + val2 + ");";
include('http://[IP]/calculate.js');
}
function include(src){
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>
Note that the last lines of each js file to be included performs an eval of
whatever value is in document.functionCall
Now this kind of works. The new versions of the functions are run from the
point they are included. Now the problem is that unfortunately the code
doesn't wait while the new function is loaded and so I am having all sorts
of problems with timing. Also having problems when including multiple js
files with functions being called from multiple files.
I am wondering if you have any idea of a better way to perform an
incremental load of .js files? Or a way to perform the overwrite of the DOM
version of the script. I was unable to find any examples of code of this
sort on your site (or any other site I've looked at) but I think it would be
a worthwhile addition to the code examples you have since it would be useful
in so many applications.
Trevor Munn
From | Me |
To | Trevor Munn |
Subject | Re: Incrementally loading script from cache or server |
Date | 5 October 2006 09:50 |
Trevor,
> I am wondering if you have any idea of a better way to perform an
> incremental load of .js files? Or a way to perform the overwrite of the
> DOM version of the script.
Not that I have ever tried this myself, but you could start out with this:
<script type="text/javascript" src="foo.js" id="bar"></script>
then use:
document.getElementById('bar').setAttribute('src','baz.js');
That may work while stalling the script until the new one loads. I do not
know if it will actually work though. I also doubt browser support will be
very high. Probably Opera, Mozilla/Firefox, and IE win only. But then, that
is about the same as what you are using already.
Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/