Venkatesh said the following on 1/8/2007 11:11 PM:
Quote:
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=[source of script]
language='JavaScript'></script>);
Doing that as the page loads is fine. Doing it after the page loads is
disastrous. So I wouldn't call that "loading on demand" per se.
Quote:
2. sc = document.createElement("<script>"); sc.setAttribute("src",
[source]); and append this to the head
Search the archives for loadJSFile and you can find more reading on that
than you probably ever cared to read about it.
Quote:
3. Get the script file using XmlHTTPRequest() and do an eval on the
text downloaded
As Rob pointed out, there are scope issues with eval'ing the text that
gets downloaded.
Quote:
etc...
>
There are few things not yet clear for me:
>
1. Which of these techniques perform synchronous load without much
effort (i.e. prevent me from writing all callbacks/event listeners and
instead allow me to write just one API function on whose return I can
be sure that javascript file is loaded).
Method 1 and Method 3.
<snip>
Quote:
2. Which of these techniques are having risks like - memory leaks etc
If any do, all of them do.
Quote:
... The reason I'm asking this is: For us every time a server hit is
made, the server response returns the list of javascript files to be
loaded. This list will not vary often but whenever the list contains
new javascript files to be loaded, I have to load those files.
When is that request made? Is it made on a setTimeout/Interval basis
while the app is running or is it made when the page is initially
loaded? How you go about dealing with loading .js files will depend -
directly - on the answer to that question.
Quote:
If I adopt method 1 for our scenario, is it fine if my load function keeps
adding the text <script src=""></scriptto document, even if the file
was downloaded earlier?
That depends on header's and cache settings. It may load twice for User1
but only once for User2.
Quote:
Or should my load function maintain a list of scripts loaded earlier
and take actions only if a request for some new javascript file is made?
That makes it sound like you are polling the server to see if new files
are available while the app is running. Without knowing when you are
checking it is impossible to come up with a definitive answer though. It
also depends on how your .js files are created/formatted.
Quote:
Request anybody having knowledge about this to throw some light.
I tried throwing light but it kept slipping through my fingers :-)
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -
http://jibbering.com/faq/index.html
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/