Jquery('#iframeid')。load()在加载Iframe CONTENT(文本,图像)之前执行

时间:2011-08-11 13:56:54

标签: javascript jquery iframe internet-explorer-8 jquery-load

我正在尝试访问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都有:相同的端口,protocoll和src。 非常感谢帮助

5 个答案:

答案 0 :(得分:3)

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

父页面:

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

Framed Page:

$(function(){
    parent.parsePeopleLink();
    //This will monitor the document being ready in the framed page 
    //and then trigger the parent's function
});

答案 1 :(得分:3)

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

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

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

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

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

答案 2 :(得分:1)

我希望隐藏等待的微调器div,当i帧内容在IE上完全加载时, 我尝试了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";
         }
     });

希望这有帮助。

答案 3 :(得分:0)

您是如何/在何处触发iframe的加载?如果您检查这些解决方案的模式:

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

有效。

类似报道的解决方案:

jQuery .ready in a dynamically inserted iframe
Javascript callback when IFRAME is finished loading?

编辑:

看起来Nick Spiers有正确的答案,或者至少它是你问题的一部分。访问iframe内容的问题。

答案 4 :(得分:-1)

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

$("#twitter-widget").load(myFunction);