Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You may write dynamic script tags (using <a href="http://www.prototypejs.org/" rel="noreferrer">Prototype</a>):</p> <pre><code>new Element("script", {src: "myBigCodeLibrary.js", type: "text/javascript"}); </code></pre> <p>The problem here is that we do not know <em>when</em> the external script file is fully loaded.</p> <p>We often want our dependant code on the very next line and like to write something like: </p> <pre><code>if (iNeedSomeMore) { Script.load("myBigCodeLibrary.js"); // includes code for myFancyMethod(); myFancyMethod(); // cool, no need for callbacks! } </code></pre> <p>There is a smart way to inject script dependencies without the need of callbacks. You simply have to pull the script via a <em>synchronous AJAX request</em> and eval the script on global level. </p> <p>If you use Prototype the Script.load method looks like this:</p> <pre><code>var Script = { _loadedScripts: [], include: function(script) { // include script only once if (this._loadedScripts.include(script)) { return false; } // request file synchronous var code = new Ajax.Request(script, { asynchronous: false, method: "GET", evalJS: false, evalJSON: false }).transport.responseText; // eval code on global level if (Prototype.Browser.IE) { window.execScript(code); } else if (Prototype.Browser.WebKit) { $$("head").first().insert(Object.extend( new Element("script", { type: "text/javascript" }), { text: code } )); } else { window.eval(code); } // remember included script this._loadedScripts.push(script); } }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload