1

I'm trying to add some script to my page creating an element with the script tag and then changing its innerHTML, it does work on Chrome and IE11 but its not working on IE8

when i change the innerHTML it just shows up like a label.

var contentScript = document.createElement("script");
contentScript.setAttribute("id", "contentSCRIPT");
contentScript.innerHTML = "alert('test');";
document.getElementById("main").appendChild(contentScript);

@Scott Mermelstein did this fiddle to help me out. http://jsfiddle.net/BjbAd/

does anyone know how to make it work on IE8? Keep in mind that i need to create the contentSCRIPT element, because I'll have to remove the script from the page after some things.

1 Answer 1

2

I think your issue is that innerHTML is not intended for use on script tags.

var contentScript = document.createElement("script");
contentScript.setAttribute("id", "contentSCRIPT");
contentScript.text = "alert('test');";
document.getElementById("main").appendChild(contentScript);
Sign up to request clarification or add additional context in comments.

1 Comment

I don't have IE10 but have tried FF, CHROME, IE8-9 all work fine. I would have thought I'd have needed to use textContent but text seems to work. Sometimes you just have to treat browsers by their limitations or let them go...

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.