1

I am using a way to insert some javascript code in the page which works with all browsers but not with IE. Here is a sample: http://boxfly.free.fr/tmp/innerjs.html

The line which does not work properly with IE is:

ele.text = document.getElementById('DIV_js').innerHTML ;

If I change this line to:

ele.text = 'alert("Hello")' ;

It works. But I need to be able to insert code which is on a lot of lines, not only one line for displaying an alert, that's why I am using a DIV to contain the Javascript code... ;)

Does anyone know how to make this script work with IE:

<body>

<div id="DIV_js" style="display:none;">
var i = 'here' ;
function hello() {
  alert('toto says '  + i) ;
}
</div>
<script>

var ele = document.createElement("script") ;
ele.type = 'text/javascript' ;
ele.text = document.getElementById('DIV_js').innerHTML ;

document.body.insertBefore(ele, (document.getElementsByTagName("div"))[0]);

</script>

<span onClick="hello();">Click to display alert</span>

</body>
Manjula
  • 4,961
  • 3
  • 28
  • 41
defacto
  • 359
  • 1
  • 5
  • 18
  • you can refer this http://stackoverflow.com/questions/5243756/innerhtml-not-working-properly-in-internet-explorer – Hemant Metalia Dec 07 '11 at 06:56
  • i think, the better way is add the script in runtime, but to the head, like google or facebook does: (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); – Alejo JM Dec 07 '11 at 07:00
  • Is it not working on IE6 or is it not working for you in IE7 and IE8 too? That example (in the link http://boxfly.free.fr/tmp/innerjs.html) worked for me in IE7 and IE8 on Win XP. – Manjula Dec 07 '11 at 07:03
  • @Manjula Yes, now it's working on IE7 to 9. I have uploaded the script if someone looks a help for the same issue. I don't test IE6 ;) – defacto Dec 07 '11 at 08:19

3 Answers3

0

See if this works in IE: http://jsfiddle.net/nGxTf/

I changed it to an eval() call, rather than inserting it into a script element.

Jake Feasel
  • 16,785
  • 5
  • 53
  • 66
0

Try this:

// The rest of the code above omitted for brievity
var ele = document.createElement("script");
ele.type = 'text/javascript';
ele.innerHTML = document.getElementById('DIV_js').innerHTML;

// Don't use getElementsByTagName, you risk inserting the script in more than one div
document.getElementById('yourDiv').appendChild(ele);
pixelfreak
  • 17,714
  • 12
  • 90
  • 109
0

innerHTML works form IE6 to IE9 but it has buggy.innerHTML on tbody elements is readOnly in IE

Ryan Yiada
  • 4,739
  • 4
  • 18
  • 20
  • It's ok, thanks to Jake it's now working with IE. I have uploaded the file, might be usefull for someone who wants to add to Javascript code to the page which is in a DIV: http://boxfly.free.fr/tmp/innerjs.html And with eval(ele.text); we don't need anymore to use document.body.insertBefore(...) :) – defacto Dec 07 '11 at 07:15