3

我正在尝试访问 iframe 的 iframe 的数据。要强制我的 javascript 函数等待,我使用 jqueries .load 函数。但是在 iframe 的内容完全加载之前,我的 javascript 代码仍然“有时”被执行。有时我的意思是不同计算机的行为不同。在速度较慢的计算机上,它等待速度较快的计算机的时间最多,代码几乎总是被执行。所以我怀疑 Internet Explorer 8 会提前触发 onload 事件。是否有任何 jquery 函数等到加载 iframe 的所有元素?

这是我的代码:

function parsePeopleLink(){
try{
    //This is where I check if the wanted I frame already exists
    if(document.getElementById('isolatedWorkArea') != null) {
        if(window.frames[2] != null) {
            if(window.frames[2].name == 'isolatedWorkArea') {
                //This is where I want the following code only to be executed when the Iframe's content is fully loaded, otherwise I get an access denied error when trying to change Userlinks
                $('#isolatedWorkArea').load(function() {
                    for( var i = 0; i < window.frames.length; i++){
                        for(var j = 0; j< window.frames[i].document.frames.length; j++){
                            IframeDocument = window.frames[i].document.frames[j].document;
                            changeUserLink(findPeopleIDfix(findPeopleID(IframeDocument)),findUserLinks(IframeDocument),8, IframeDocument);
                        }
                    }
                });
            }
            else {
                throw e; //I know that all the nested else statements are ugly, I just inserted them for testing purposes
            }   
        }
        else {
            throw e;
        }
    }
    else {
        throw e;
    }
}
catch(e) {
    //alert(e.description);
    for(var k = 0; k < window.frames.length; k++)
    {
        IframeDocument = window.frames[k].document;
        changeUserLink(findPeopleIDfix(findPeopleID(IframeDocument)),findUserLinks(IframeDocument),9, IframeDocument);      
        iframeindex++;
    }
  }
}

所有 iframe 都具有:相同的端口、协议和 src。非常感谢帮助

4

5 回答 5

3

Javascript 无法访问外部内容以在完全加载时进行监控。如果您有权访问 iframe 内容,则可以添加对父窗口函数的调用以触发它。

父页面:

function parsePeopleLink() {
    //all your current code like you have it now
}

框架页面:

$(function(){
    parent.parsePeopleLink();
    //This will monitor the document being ready in the framed page 
    //and then trigger the parent's function
});
于 2011-08-11T14:09:23.380 回答
3

我在我的一个项目中使用了类似的东西。我正在尝试下载文件,并在下载完成后显示消息。这种事情在我的经验中不起作用:

$('#myiframe').attr("src","http://example.com");
$('#myiframe').load(function() { /* do work on load */ });

Load 事件在 iframe 以 HTML 编写后立即触发,而不是在 iframe 加载其所有内容时触发。但是这段代码有效:

$('#myiframe').load("http://example.com", function() { /* do work on load */ });

此代码等待我的文件下载然后触发加载事件。我认为这也适用于你。

于 2012-12-12T12:07:23.030 回答
1

当 i 框架内容在 IE 上完全加载时,我想隐藏等待的微调器 div,我尝试了 Stackoverflow.Com 中提到的所有解决方案,但没有按我的意愿工作。

然后我有了一个想法,当 i 帧内容完全加载时,可能会触发 $(Window) 加载事件。这正是发生的事情。所以,我写了这个小脚本,并且像魔术一样工作:

$(window).load(function () {
         //alert("Done window ready ");
         var lblWait = document.getElementById("lblWait");
         if (lblWait != null ) {
             lblWait.style.visibility = "false";
             document.getElementById("divWait").style.display = "none";
         }
     });

希望这可以帮助。

于 2014-12-10T08:00:31.890 回答
0

您如何/在哪里触发 iframe 的负载?如果您检查这些解决方案的模式:

$('#myiframe').attr('src', 'http://example.com');
$('#myiframe').load(function() { /* do work on load */ });

据报道工作。

类似报告的解决方案:

当 IFRAME 完成加载时,jQuery .ready 在动态插入的 iframe Javascript 回调中准备好了吗?

编辑:

看起来尼克斯皮尔斯有正确的答案,或者至少这是你问题的一部分。iframe 内容的访问问题。

于 2011-08-11T14:02:15.320 回答
-1

我使用此代码确保在运行我的函数之前加载了 iFrame(由它的 ID 标识):

$("#twitter-widget").load(myFunction);
于 2014-07-02T17:32:32.990 回答