5

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?

5
  • You are probably better off using jquery since you won't have to worry about certain features implemented differently in IE. Commented Jan 2, 2013 at 1:38
  • If you know of a way to do this with jQuery, please let me know. $(node).html isn't working any better than script.innerHTML. Commented Jan 2, 2013 at 1:39
  • Maybe this helps: stackoverflow.com/questions/8610574/… Commented Jan 2, 2013 at 1:40
  • @regulatethis, both the solutions on that question use innerHTML, which doesn't work in IE. Commented Jan 2, 2013 at 1:42
  • 1
    Looks like there are some security restrictions with IE, here is a discussion about it. stackoverflow.com/questions/703705/… Commented Jan 2, 2013 at 1:42

1 Answer 1

6

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);
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.