« Previous day (Jun 24, 2007) | Main | Next day (Jun 26, 2007) »
http://blogs.sun.com/insidemyhead/date/20070626 Tuesday June 26, 2007

Dynamic JavaScript Loading

While developing a custom jMaki widget we came across a problem in which we needed to load a javascript dynamically from another javascript file. There are atleast 2 ways I know this can be done. Here is one such method.

 

function jsinclude( jscriptfile )
{

// create a script node
var s = document.createElement('script');
s.src = jscriptfile;
s.type = "text/javascript";

// add it to the node called 'SOMEPARENT'.. it could be the HEAD node of the html page as below....

document.getElementsByTagName('head').item(0).appendChild(s);
}

Calling this function in your javascript you can load as many JS files as needed and embed them into your current DOM tree as follows :


jsinclude('dojofunctions.js');




Posted by insidemyhead [Sun] ( June 26, 2007 09:21 AM ) Permalink | Comments[4]