[Javascript] Dynamically adding script tags, IE.

Hakan M. hakan at backbase.com
Wed Jan 14 08:08:20 CST 2004


Cheers.

I'm having a problem with dynamic loading of scripts. We have to do it a 
bit ugly, using document.body.innerHTML to add strings containing 
'<script src="blabla" type="text/javascript"></script>'. There is a 
working solution (running over a list of sources and assigning the 
source to an existing script tag) but we can't use that.

The problem is that IE doesn't execute/parse the tag (or rather, the 
file in the src attribute) when we write it to the body using innerHTML. 
HOWEVER, if we add '<script 
type="text/javascript">alert("something");</script>' to the strings we 
pass to innerHTML, the alert gets fired AND the other scripts suddenly 
gets loaded and parsed.

It seems like throwing an (inline) alert makes IE understand that it's 
supposed to parse the sources of the script tags, without this alert 
(I've tried writing to the statusbar, creating something, running a 
function - only alert does the trick) nothing happens.

Naturally, we've been experimenting with the DEFER attribute, but it 
makes no difference. Also, if that would be the case, I can't see how an 
alert (and NO OTHER CODE WHATSOEVER) should "fix" it.

Anybody with hardcore insight in why IE behave this way?

Thanks,
Hakan




More information about the Javascript mailing list