简体   繁体   English

如何从iframe内部确定父页面是否已完成加载?

[英]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? 是否可以通过iframe监听以确定是否有任何属性或事件?

The iframe src and parent page are on different domains. iframe src和父页面位于不同的域中。

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. 如果不能,那就是XSS,并且没有合法的方法。

parent.html: parent.html:

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

frame.html:* 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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