Wednesday, May 12, 2010

Include JS File from JS

function loadScript(url, callback)
{
// adding the script tag to the head as suggested before
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script
.type= 'text/javascript';
script
.src= url;

// then bind the event to the callback function
// there are several events for cross browser compatibility
script
.onreadystatechange = callback;
script
.onload = callback

// fire the loading
head
.appendChild(script);
}
loadScript("my_lovely_script.js", myPrettyCode);
Alternatively, use
$.getScript("my_lovely_script.js", function(){


alert
("Script loaded and executed.");
// here you can use anything you defined in the loaded script

});

No comments:

Post a Comment