首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IFrame OnReadyStateChange函数

IFrame OnReadyStateChange函数
EN

Stack Overflow用户
提问于 2013-10-15 23:05:43
回答 2查看 16.3K关注 0票数 4

我有一个asp.webforms应用程序,在页面a上,我有一个隐藏的div,里面有进度条和iframe。对于iframe,我尝试从同一域上的另一个应用程序加载表单。

代码语言:javascript
复制
<div id="pagePreview" style="display: none;">
            <div class="progressBarWrapper" id="waitDialog" style="opacity:1;filter:alpha(opacity=100);display:none;">
                <div class="progressBarDetail" style="margin-top:25%;">
                    <asp:Image ID="imgLoading" runat="server" ImageUrl="~/Images/wait.gif" />
                </div>
            </div>
            <iframe id="previewContent" onreadystatechange="iframeLoaded(this);"></iframe>
        </div>

在单击事件中,我调用一个函数在jqueryUI对话框中显示此div,并希望显示进度条,直到Iframe中的页面未加载。

代码语言:javascript
复制
var isClickedForDialog = false;

function iframeLoaded(args) {
            if (args.readyState == "complete" && isClickedForDialog) {
                var pagePreview = $('#pagePreview'); // dialog
                var waitDialog = $('#waitDialog'); // progress

                waitDialog.hide();

                isClickedForDialog = false;
            }
        }

function showModalWindow(url, hideCloseButton) {
            isClickedForDialog = true;

            var previewContent = $('#previewContent'); // Iframe
            var pagePreview = $('#pagePreview'); // dialog
            var waitDialog = $('#waitDialog'); // progresss

            waitDialog.show();

            previewContent.attr('src', url); 

            pagePreview.dialog(
                    {
                        draggable: false,
                        resizable: false,
                        height: 764,
                        width: 1020,
                        modal: true,
                        close: function (event, ui) {
                            previewContent.attr('src', '');
                        },
                        open: function (event, ui) {
                            if (hideCloseButton) {
                                $(this).parent().children().children('.ui-dialog-titlebar-close').hide();
                            }
                        }
                    });
        }

在IE中,一切运行正常。对话框和进度栏显示,当网址加载到iframe中时,进度栏消失,我在IFrame中只看到webforms。

但在FireFox和Chrome中,这是行不通的。

浏览器忽略onreadystatechange事件。我尝试按如下方式处理事件:

代码语言:javascript
复制
$('#previewContent').bind('onreadystatechange', iframeLoaded, false); 
$('#previewContent').on('onreadystatechange', iframeLoaded);

但没有成功。

知道怎么解决这个问题吗?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-15 23:32:22

我不确定你使用onreadystatechange是否有什么特别的原因,但是如果你只是想知道iframe什么时候加载完成,load事件会处理的。

代码语言:javascript
复制
$('#previewContent').on('load', iframeLoaded);
票数 5
EN

Stack Overflow用户

发布于 2016-04-15 14:52:51

iframe标记添加onreadystatechange属性(如原始问题中所示)似乎没有任何作用。不要这样做:

代码语言:javascript
复制
<iframe onreadystatechange="iframeReady(this);"></iframe>

相反,获取对iframe元素的引用,并将DOMContentLoaded侦听器添加到其contentDocument属性。因为你的iframe可能已经装满了,你应该检查它的contentDocumentreadyState,如果iframe还没有装入,你应该取消监听器。最后,一些浏览器--比如火狐--目前不会从iFrame发出DOMContentLoaded事件,所以为了回退,你可以在iframecontentWindow属性上添加一个load监听器,或者在iframe本身上添加一个监听器。

代码语言:javascript
复制
function listenForIframeReady() {
  if (iframe.contentDocument.readyState === "interactive" || iframe.contentDocument.readyState === "complete") {
    iframeReady();
  } else {
    iframe.contentDocument.addEventListener('DOMContentLoaded', iframeReady);
    iframe.contentWindow.addEventListener('load', iframeReady);
    iframe.addEventListener('load', iframeReady);
  }
}

function iframeReady() {
  console.log('iframe is ready');
  iframe.contentDocument.removeEventListener('DOMContentLoaded', iframeReady);
  iframe.contentWindow.removeEventListener('load', iframeReady);
  iframe.removeEventListener('load', iframeReady);
}

var iframe = document.querySelector('iframe');
listenForIframeReady();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19384661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档