Published in: JavaScript
/** * Only returns true when the external script has been * loaded in to the DOM. It uses arguments.callee.caller * to work out which function is the callback. * * @param url {String} URL of external script * @param obj {String} The name of a function or variable * within the external script to test for. * @license: Creative Commons License - * ShareAlike http://creativecommons.org/licenses/by-sa/3.0/ * @author Remy Sharp / leftlogic.com */ function waitingForScript(url, obj) { // doesn't work in Opera var callback = arguments.callee.caller; var args = arguments.callee.caller.arguments; var s, ok, timer, doc = document; // if the object/function doesn't exist and we've not tried to load it // then pull it in and fire the calling function once complete if ((typeof window[obj] == 'undefined') && !window['loading' + obj]) { window['loading' + obj] = true; if (!doc.getElementById('_' + obj)) { s = doc.createElement('script'); s.src = url; s.id = '_' + obj; doc.body.appendChild(s); } timer = setInterval(function () { ok = false; try { ok = (typeof window[obj] != 'undefined'); } catch (e) {} if (ok) { clearInterval(timer); callback.apply(this); } }, 10); // we're loading in the script now, so we're currently waiting return true; } else if (typeof window[obj] == 'undefined') { // object not defined yet, so we're still waiting return true; } else { // it's already loaded return false; } }
You need to login to post a comment.
