Run script before iframe loads
asynchronous, html, iframe, javascript
Solution
One thing that you can do is set the source of the iFrame after the script loads.
So, create a function that will load the script on page load, and then once the scrip is completed you can set a callback function which will execute after your script is done loading.
function loadScript( url, callback ) {
var script = document.createElement( "script" )
script.type = "text/javascript";
if(script.readyState) { //IE
script.onreadystatechange = function() {
if ( script.readyState === "loaded" || script.readyState === "complete" ) {
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function() {
callback();
};
}
script.src = url;
document.getElementsByTagName( "head" )[0].appendChild( script );
}
// call the function...
loadScript('change-document-domain.js', function() {
//executes after the script is laoded.
document.getElementById("myFrame").src = "sourceOfIFrame.html"
});
Problem
I'm working with a help system which is embedded in an application. The important part of the help tries to have the same document.domain value, but the child iframe seems to run its document.domain setting before the parent does. This is a problem because it throws a security error and halts javascript execution. Here's basically what the html looks like, to give the proper idea: ``` <html> <head> <!--Occasionally runs second--> <script src="change-document-domain.js"></script> </head> <body> <iframe> <html> <head> <!--Occasionally runs first--> <script src="change-document-domain.js"></script> </head> </html> </iframe> </body> </html> ``` The help is very restricted by the program which builds it. Is there any change I can make to the parent script or parent script tag to make it load before the iframe or its script does? Let me know if you have any questions, and thanks as always!