Why does appending a to a dynamically created seem to run the script in the parent page?

Had the same problem, took me hours to find the solution.
You just need to create the script’s object using the iframe’s document.

var myIframe = document.getElementById("myIframeId");
var script = myIframe.contentWindow.document.createElement("script");
script.type = "text/javascript";
script.src = src;
myIframe.contentWindow.document.body.appendChild(script);

Works like a charm!

Leave a Comment