Dynamically loading Javascript files

0 Comments

The little snippet below lets you load JS files dynamically, making it easier to load jQuery or mootools for scripts like GreaseMonkey or within Firebug.

var jsfile = document.createElement("script");
jsfile.setAttribute("type", "text/javascript");
jsfile.setAttribute("src", "
http://ajax.googleapis.com/ajax/libs/mootools/1.2.2/mootools.js");
document.getElementsByTagName("head")[0].appendChild(jsfile);

Using Firebug, I used the following mootools code to hide some elements as a test to see if the script actually loaded.

$each($$('.saveType, .perType'), function(item) {
  item.setStyle('display', 'none');
});

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog