How to run a script that has been added to a page using innerHTML?

Possible duplicate:
Execution of <script> elements inserted with .innerHTML
Dynamic installation of <script> tags in HTML on page load

I mean, if I record .innerHTML containing

<script type="text/javascript" src="source.js"></script>

or

<script type="text/javascript"> // embedded code here </script>

Nested code does not run, and it is also not associated with code. He is dead".

Is there any way to start it manually?

+3
source share
1 answer

you need to add javascript to the header, i.e.

var head = document.getElementsByTagName("head")[0];         
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = 'http://www.somedomain.com/somescript.js';
head.appendChild(newScript);

(this is a pretty common thing, but I copied the code here: http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_And_CSS )


: jQuery, :

<script>
    [....]
    $( "head" ).append( "<script src='myScript.js'></script>" ); 
    [....]
</script>

, , javascript </script> .

+3