简体   繁体   中英

From inside an iframe, how can I determine if the parent page has finished loading?

Are there any properties or events I can listen for from within the iframe to determine this?

The iframe src and parent page are on different domains.

Only if you can modify the parent page as well. If you can't, it's XSS and there's no legitimate way to do it.

parent.html:

...
<body onload="theFrame.parentLoaded()">
...
<iframe src="http://other.domain.com/frame.html" name="theFrame"></iframe>
...

frame.html:*

...
<script type="text/javascript" language="javascript">
function parentLoaded(){
    //code here will be executed when the parent page has finished loading
}
</script>
...

I know you don't have control over the parent page, but if you can convince the host to add some code, you may be able to do something like the following:

//OnParent Page
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script>
    var bReady = false;
    $(document).ready(function () {
        bReady = true;
    });

    function isReady() {
        return bReady;
    }
</script>

//in iframe page
<script>
    var bReady = false;
    while (bReady == false) {
        bReady = window.opener.isReady();
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM