javascript - document.createElement('script')... adding two scripts with one callback -
I have to add prototype and then add scriptaculous and get a callback when they will be loaded. I am currently loading prototypes like this:
var script = document.createElement ("script"); Script.src = "http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js"; Script.onload = script.onreadystatechange = callback; Document.body.appendChild (script); I can do this by screaming the callback, but it seems that poor practice (when I need to load more scripts I do not want the silly series of 20 callback methods). idea?
I propose to use some small loaders that will make things and accessories for you. For example, in this way:
Load the function (array, callback) {var loader = function (src, handler) {var script = document.createElement ("script"); Script.src = src; Script.onload = script.onreadystatechange = function () {script.onreadystatechange = script.onload = null; Handler (); } Var head = document.getElementsByTagName ("head") [0]; (Head || document.body) .appendChild (script); }; (Function run () {if (array.length! = 0) {loader (array.shift (), run);} else {callback & callback ();}}) (); }
This script should help you create a script tag and call your callback when all files are loaded. Insert is very easy:
Load (["http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js", "http : / /ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js "], function () {warning ('all things are loaded');});
Hope this will help
Comments
Post a Comment