Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way to do this is to write two &lt;script&gt; blocks.</p> <pre><code>&lt;script type="text/javascript"&gt; document.write(decodeURI("%3Cscript src='something.js' type='text/javascript'%3E%3C/script%3E")); &lt;/script&gt; &lt;script type="text/javascript"&gt; myFunction(); &lt;/script&gt; </code></pre> <p>When <strong>document.write</strong> is used, the text is written directly into the document, right after the &lt;script&gt; tag. Because of this, when a browser encounters a &lt;script&gt; tag, it waits for the script to finish loading before continuing down the page. Any <strong>document.write</strong> that happens in the loaded script might affect the rest of the page (for example, if you do "document.write('&lt;div&gt;')", it will affect the rest of the layout).</p> <p>So, if you use two &lt;script&gt; tags, the first one is encountered by the browser, and it uses <strong>document.write</strong> to output a new &lt;script&gt; tag. As soon as that &lt;script&gt; tag is finished, the browser continues down the page, and immediately encounters the new, dynamically added &lt;script&gt; tag. This &lt;script&gt; tag tells the browser to load your external Javascript. The browser will wait to execute the second original &lt;script&gt; tag until this &lt;script&gt; tag has finished loading. Then your function <strong>myFunction</strong> will be available.</p> <p>The reason doing it all in one &lt;script&gt; tag doesn't work is because <strong>document.write</strong> doesn't happen until after the &lt;script&gt; has finished running (<strong>myFunction</strong> isn't available yet). The reason it works with two &lt;script&gt; tags is because the browser waits until each &lt;script&gt; tag runs in succession, and <strong>document.write</strong> puts a new &lt;script&gt; tag in line.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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