javascript - How to dynamically add scripts to webpage from an HTML string? -


say have string named html has in it:

<script>     document.write("some random stuff here"); </script>  <script src="someremotejsfile"></script> 

i want display within iframe window dynamically.

my original solution do:

document.open(); document.write(html); document.close(); 

but causes problems in firefox spinner keeps spinning if loading forever though content has loaded. next attempt to:

document.body.innerhtml = html; 

this adds scripts body, doesn't execute them. lastly tried:

div = document.createelement("div"); div.innerhtml = html; document.body.appendchild(div); 

but doesn't seem execute scripts inside html string.

so question is, given string of html, how dynamically add page? instance, can ad tag has number of scripts , other html elements in it. have no control on html string has in it. it's black box me. have able take long string of html , load window (an iframe in case).

document.write works:

<iframe id="ifr"></iframe>  <script type="text/javascript">     var scr = decodeuricomponent("%3ch1%3ehello%20world%3c%2fh1%3e%3cscript%3ealert(%27some%20random%20stuff%20here%27)%3b%3c%2fscript%3e");     document.getelementbyid("ifr").contentwindow.document.write(scr);    document.getelementbyid("ifr").contentwindow.document.close();  </script> 

(never mind encoded uri string, needed able assign code <h1>hello world</h1><script>alert('some random stuff here');</script> string variable inside of script tags


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -