Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer varies depending on where the script tag is and how you've added it:</p> <ol> <li><p>Script tags inline with your markup are executed synchronously with the browser's processing of that markup (except, see #2), and so if -- for instance -- those tags reference external files, they tend to slow down the processing of the page. (This is so the browser can handle <code>document.write</code> statements, which change the markup they're processing.)</p></li> <li><p>Script tags with the <code>defer</code> attribute may, on some browsers, not be executed until after the DOM has been fully rendered. Naturally these can't use <code>document.write</code>. (Similarly there's an <code>async</code> attribute that makes the script asynchronous, but I don't know much about it or how well it's supported; <a href="http://dev.w3.org/html5/spec/semantics.html#script" rel="noreferrer">details</a>.)</p></li> <li><p>Script tags in content you assign to elements after DOM load (via <code>innerHTML</code> and similar) are not executed at all, barring your use of a library like jQuery or Prototype to do it for you. <em>(With one exception pointed out by Andy E: On IE, if they have a <code>defer</code> attribute, it will execute them. Doesn't work in other browsers.)</em></p></li> <li><p>If you append an actual <code>script</code> element to the document via <a href="https://developer.mozilla.org/En/DOM/Node.appendChild" rel="noreferrer"><code>Element#appendChild</code></a>, the browser begins downloading that script immediately and will execute it as soon as the download is finished. Scripts added this way are not executed synchronously or necessarily in order. First appending a <code>&lt;script type="text/javascript" src="MyFct.js"&gt;&lt;/script&gt;</code>, and then appending <code>&lt;script type="text/javascript"&gt;myFunction();&lt;/script&gt;</code> may well execute the inline (second) one before the remote (first) one. If that happens and <code>MyFct.js</code> declares <code>myFunction()</code>, it will not be defined when we try to use it with the inline script. If you need things done in order, you can tell when a remote script has been loaded by watching the <code>load</code> and <code>readyStateChange</code> events on the <code>script</code> element you add (<code>load</code> is the event on most browsers, <code>readyStateChange</code> on some versions of IE, and some browsers do both, so you have to handle multiple notifications for the same script).</p></li> <li><p>Script inside event handlers on attributes (<code>&lt;a href='#' onclick='myNiftyJavaScript();'&gt;</code>) rather than in a script tag is executed when the relevant event occurs.</p></li> </ol> <hr> <p>I was working away at my Real Job and suddenly my hindbrain said "You know, you've been <em>told</em> they won't be executed if you assign them to <code>innerHTML</code>, but have you personally checked?" And I hadn't, so I did -- FWIW:</p> <pre><code>&lt;!DOCTYPE HTML&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-type" content="text/html;charset=UTF-8"&gt; &lt;title&gt;Script Test Page&lt;/title&gt; &lt;style type='text/css'&gt; body { font-family: sans-serif; } &lt;/style&gt; &lt;script type='text/javascript'&gt; function addScript() { var str, div; div = document.getElementById('target'); str = "Content added&lt;" + "script type='text/javascript'&gt;alert('hi there')&lt;" + "/" + "script&gt;"; alert("About to add:" + str); div.innerHTML = str; alert("Done adding script"); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt;&lt;div&gt; &lt;input type='button' value='Go' onclick='addScript();'&gt; &lt;div id='target'&gt;&lt;/div&gt; &lt;/div&gt;&lt;/body&gt; &lt;/html&gt; </code></pre> <p>The alert from the script doesn't appear on IE7, FF3.6, or Chrome4 (I haven't bothered to check others, I'm meant to be working :-) ). Whereas if you append elements as shown <a href="http://proto-scripty.wikidot.com/prototype:how-to-load-scripts-dynamically" rel="noreferrer">here</a>, the script gets executed.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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