views:

964

answers:

4

Hi,

I'm trying to do the following (I'm using the prototype library):

var div = document.createElement('div');
div.innerHTML = '<script src="somescript.js"></script>';
$('banner').insert(div);

In IE, div.innerHTML property is always equal to "" after I set the property in the second line.

This snippet is inside a function which is overriding document.write() in an external vendor script, so that is why I am doing it this way rather than creating a script element and appending it to the div element directly.

Any help would really be appreciated, this is giving me grey hairs!

A: 

Have you tried to add inline JS instead of loading a .js file? I've done this in the past and it worked fine for me. Not sure if that would still work with the lastest browsers / security missery.

HTH.

Jonathan van de Veen
+3  A: 

You could try to do something like this instead:

function loadScript(src) {
       var script = document.createElement("script");
       script.type = "text/javascript";
       document.getElementsByTagName("head")[0].appendChild(script);
       script.src = src;
}

or do

..
div.innerHTML = "<script src=\"somescript.js\"></script>";
..
Juri
+1 for the first option.
Justin Niessner
I also prefer the 1st one. It is much cleaner and reusable. They 2nd was just a "try-it" suggestion if he doesn't want to use the 1st approach :)
Juri
I have a vague feeling the OP's problem is actually a security feature at work and your first answer here is the conventional (and non-blocking!) solution.
annakata
Agreed, that is how I would normally do it but as I said there is an external vendor script that is doing:document.write('<script src="somescript.js"></script>');I don't know what the src will be. We are calling this script after a partial page refresh (ajax) and the document.write renders a new page, therefore I am overriding document.write in my script to try and append the script to the page rather than allow the document.write to happen. I should have been clearer about the circumstances. Does that all make sense?
paulie
+3  A: 

Your script tag is probably managing to be interpreted independently. Try:

div.innerHTML = '<scr' + 'ipt src="somescript.js"></scr' + 'ipt>';
chaos
This is a common problem with using script tags in code and very likely the problem here
John Burton
This is interesting. Can you elaborate?
paulie
+1 This sounds like the issue to me.
RedFilter
Quite a few browsers mangle obvious script injections like this. The work-around is correct, though - mangle it yourself in a way that still works.
Jeff Ober
Tried the following in IE - using IE8 in IE7 mode and developer Tools debugger, value does not end up in div:<html><body><div id="test"></div></body><script type="text/javascript"> var div = document.createElement('div'); div.innerHTML = '<scr' + 'ipt src="somescript.js"></scr' + 'ipt>'; document.getElementById('test').appendChild(div); </script></html>
paulie
I suspect you may need to set the innerHTML after the div is already in the DOM (after the appendChild) in order for the script to work.
chaos
that doesn't seem to make a difference in the above example.
paulie
Okay. I guess interpretation of the script tag isn't the issue, then.
chaos
A: 

Your quotes are wrong on the second line. Should be:

div.innerHTML = '<script src="somescript.js"></script>';
RedFilter
Typo, whoops. Have amended.
paulie