'Run script before iframe loads

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!



Solution 1:[1]

Not sure about whether you want this kind of solution or not. But using javascript or jquery for this purpose might be a workaround.

<iframe id="frame" src="">
</iframe>

And you just trigger the iFrame load after the parent content javascript is loaded ...

 $("#frame").prop("src", "http://iframe-url.com/");

Solution 2:[2]

try creating a separate page containing the content for the iframe and like to it, which could be slightly slower, but would load in order.

Solution 3:[3]

Based on runcorn's answer (which did not do it for me), add a time delay (of one second) by wrapping in a function:

setTimeout(function() { 
$("#frame").prop("src", "http://iframe-url.com/");;
}, 1000);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Runcorn
Solution 2 Sam Denton
Solution 3 ACKmiecik