Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This one had me stymied for a bit as well. It turns out that IE does not allow the insertion of JS directly via innerHTML unless you include the 'defer' property (see the second link below). This property is unique to IE and apparently allows IE to defer execution of any JS until after the other markup has been loaded. A warning, though...if you include two script tags (as I did), there is no guarantee which one will execute first, as the scripts appear to be loaded asynchronously. This should only be a problem if your scripts are dependent on one another (as mine were). </p> <p>There is an additional caveat as well...you must insert non-script markup at the same time that you insert the script. I was unable to insert the script tags by themselves, with or without the 'defer' property. Finally, the script tags must be placed after all other non-script markup being inserted. Otherwise, the script tags are stripped out of the inserted HTML. </p> <p>Here are a few references:</p> <p>MS innerHTML Reference:</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms533897%28v=vs.85%29.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms533897%28v=vs.85%29.aspx</a></p> <p>MS Defer Property Reference:</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms533719%28v=vs.85%29.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms533719%28v=vs.85%29.aspx</a></p> <p>Example of Script Insert via code (yes, it actually does work): </p> <p><a href="http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/insertScript_2.htm" rel="noreferrer">http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/insertScript_2.htm</a></p> <p>My Test Code:</p> <pre><code>// I downloaded the MS example file above and tweaked their script a bit, // resulting in this. Using the proper approach to the defer property // (namely: defer="defer") did not provide me with consistent results, so // sticking with 'DEFER' may be necessary. // Note: Try moving the 'sHTML' variable to the end of the script string. function insertScript2() { var sHTML="&lt;input type=button onclick=" + "go2()" + " value='Click Me'&gt;&lt;BR&gt;"; var sScript = sHTML + "&lt;SCRIPT DEFER type='text/javascript'&gt; function go2(){ alert('Hello from inserted script.') } &lt;/SCRIPT" + "&gt;"; ScriptDiv.innerHTML = sScript; } </code></pre>
    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. 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.
    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