V
Venkatesh
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>);
2. sc = document.createElement("<script>"); sc.setAttribute("src",
[source]); and append this to the head
3. Get the script file using XmlHTTPRequest() and do an eval on the
text downloaded
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 3 is kind of synchronous but I'm not able to use eval() inside a
function because the scope of eval is within the scope of the function
calling the eval, effectively making the functions inside the loaded
javascript file inaccessible to the rest of the code.
2. Which of these techniques are having risks like - memory leaks etc
.... 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. If I
adopt method 1 for our scenario, is it fine if my load function keeps
adding the text <script src=""></script> to document, even if the file
was downloaded earlier? 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?
Request anybody having knowledge about this to throw some light.
Thank you,
Venkatesh
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>);
2. sc = document.createElement("<script>"); sc.setAttribute("src",
[source]); and append this to the head
3. Get the script file using XmlHTTPRequest() and do an eval on the
text downloaded
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 3 is kind of synchronous but I'm not able to use eval() inside a
function because the scope of eval is within the scope of the function
calling the eval, effectively making the functions inside the loaded
javascript file inaccessible to the rest of the code.
2. Which of these techniques are having risks like - memory leaks etc
.... 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. If I
adopt method 1 for our scenario, is it fine if my load function keeps
adding the text <script src=""></script> to document, even if the file
was downloaded earlier? 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?
Request anybody having knowledge about this to throw some light.
Thank you,
Venkatesh