insanepaul
insanepaul

Reputation: 197

document.body.innerHTML strips javascript in IE - bug?

I want to parse tags like and into document.body.innerHTML (see below). However although you can do this in Chrome, it doesn't work in IE.I have to get it to work in IE butit seems that isn't possible. Any ideas?

html = '<script> function myFunction() { alert(); } </script>AAA'

doc.body.innerHTML = html

AAA is stored in doc.body.innerHTML

To quickly replicate the issue just add the first line below to the IE console window then see what is in document.body.innerHTML and you will notice its been stripped.

We use a 10 year old text editor which will only work in IE and has always stripped javascript so now I've discovered why and was hoping someone could provide an alternative...googling isn't helping much

document.body.innerHTML = "<script> function myFunction() { alert(); } </script>AAA"

document.body.innerHTML

"AAA"

Upvotes: 0

Views: 960

Answers (2)

codebreaker
codebreaker

Reputation: 1485

Hi try like below code might help u out..

you need to use escape char for the </script> tag

div.innerHTML = '<script> function myFunction() { alert(); } <\/script>AAA';

Upvotes: 1

Ramesh
Ramesh

Reputation: 4293

This will work on any browser to insert javascript:

var newScript = document.createElement('script');
var scriptTag = document.getElementsByTagName('script')[0];
newScript.text = "function myFunction() { alert(); }"
scriptTag.parentNode.insertBefore(newScript, scriptTag);

Upvotes: 1

Related Questions