Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to inject javascript into my page

For reasons too complicated to get into now, I have an ajax call that returns some dynamically created Javascript that I want to inject into my page. The following code works on Chrome, but not in IE:

 var node = document.getElementsByTagName("head")[0] || document.body;
  if (node)
  {
    var script = document.createElement("script");
    script.type = "text/javascript";
    //script.innerHTML = json.javascript;
    var textnode = document.createTextNode(json.javascript);
    script.appendChild(textnode);
    node.appendChild(script);
  }

In IE, I get "SCRIPT65535: Unexpected call to method or property access." As you can see from the commented out code, before I tried the textnode, I tried just inserting it with script.innerHTML. That also worked in Chrome, but in IE I got "SCRIPT600:Unknown runtime error".

Is there a way to stick some javascript into the DOM in IE?

like image 850
Paul Tomblin Avatar asked Jan 02 '13 01:01

Paul Tomblin


1 Answers

And of course, as soon as I post this, I find http://www.phpied.com/dynamic-script-and-style-elements-in-ie/

  var node = document.getElementsByTagName("head")[0] || document.body;
  if (node)
  {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.text = json.javascript;
    node.appendChild(script);
  }
like image 129
Paul Tomblin Avatar answered Sep 27 '22 23:09

Paul Tomblin