0

i have the following code on my html page:

<SCRIPT type="text/javascript">
function insertScript()
{
    var sHTML="<input type=button onclick=" + "go2()" + " value='Click Me'><BR>";
    var sScript="<SCRIPT DEFER>";
    sScript = sScript + "function go2(){ alert('Hello from inserted script.') }";
    sScript = sScript + "</SCRIPT" + ">";
    ScriptDiv.innerHTML = sHTML + sScript;
}
</SCRIPT>

its working fine and showing the alert on IE9, but not on IE11.

'go2' is undefined

i have this example on the following link, any help in this regard is greatly appriciated:

http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/insertScript_2.htm

Vikram
  • 8,235
  • 33
  • 47
  • 2
    I don't see the need for DEFER here, DEFER means it doesn't load it at once, it's just queued behind all other ones who don't have defer. It also doesn't work on FF if it helps ;) – Icepickle Jan 15 '16 at 14:42
  • You're referencing `ScriptDiv` in the global scope. Try `document.getElementById('ScriptDiv').innerHTML = ...` – MrWhite Jan 15 '16 at 14:43
  • I think `innerHTML` adds the script tag but it doesn't really do anything (doesn't get run). You'll need to add a `script` element instead, although this is pointless. – MinusFour Jan 15 '16 at 14:45
  • this is a small part of a bigger problem, i need DEFER. this is a microsoft example, and its not working for IE11, while its working on ie9. – Vikram Jan 15 '16 at 14:53
  • Yeah, because IE9 doesn't know DEFER yet? ;) – Icepickle Jan 15 '16 at 14:55
  • @Vikram You do see that the example is from 2007, no? Some things change overtime – Icepickle Jan 15 '16 at 14:59
  • Related: [Can scripts be inserted with innerHTML?](http://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml) – MrWhite Jan 15 '16 at 15:27
  • yes the example is from 2007, but deffer is working in IE9. for proof you can remove the defer from the script and then try, it will throw the same error. – Vikram Jan 18 '16 at 08:02

2 Answers2

3

You need to move your javascript code to a .js file and then include that file using src attribute then this will work.

e.g. move java script code to test.js then include file like below.

<script defer src="\\Test.js">

This is convention in IE 11 to use defer attribute. Defer cannot run without src attribute in script tag.

1

A way that it seems to work, would be like this

I add the element using document.createElement instead, and append it to the to a target element, or to the document.body in case the targetElement is not there

There are some syntax errors that you might check for in your original code, but this does the trick (don't know what you originally try to solve though, this is a horrible way of adding scripts to a page)

And there is really no need for "DEFER", you might need to wait till your page is loaded (so attach yourself to the load event)

function insertScript(targetElement)
{
  var button = document.createElement('input');
  button.setAttribute('type', 'button');
  button.setAttribute('onclick', 'go2()');
  button.value = 'Click me';
  
  var sScript= document.createElement('script');
  sScript.innerHTML = "function go2(){ alert('Hello from inserted script.'); }";
  
  (targetElement || document.body).appendChild(button);
  (targetElement || document.body).appendChild(sScript);
}

insertScript(document.getElementById('target'));
<div id="target"></div>
Icepickle
  • 12,689
  • 3
  • 34
  • 48