Jquery ('#iframeid').load() 在加载 Iframe 内容(文本、图像)之前执行

发布于 2024-11-29 02:54:17 字数 1896 浏览 2 评论 0原文

我正在尝试访问 Iframe 的 Iframe 的数据。为了强制我的 javascript 函数等待,我使用 jqueries .load 函数。但我的 javascript 代码仍然“有时”在 iframe 的内容完全加载之前执行。有时我的意思是不同的计算机的行为是不同的。在较慢的计算机上,它大多数时间都会等待较快的计算机,代码几乎总是被执行。所以我怀疑 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。 非常感谢帮助

I'm trying to access data of an Iframe's Iframe. To force my javascript function to wait I use jqueries .load function. But still my javascript code get's executed "sometimes" before the iframe's content is fully loaded. By sometimes I mean that the behavior is different for different computers. On slower computers it waits most times for fasters computers the code gets almost always executed. So I suspect that Internet explorer 8 fires the onload event to early. Is there any jquery function to wait until all elements of an iframe are loaded?

This is my code:

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++;
    }
  }
}

All Iframes have: same port, protocoll, and src.
Help is much appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

韶华倾负 2024-12-06 02:54:17

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
});

Javascript can't access external content to monitor when it's completely loaded. If you have access to the iframe content you can add a call to the parent window's function to trigger it.

Parent Page:

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
});
泪冰清 2024-12-06 02:54:17

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

$('#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 */ });

这段代码等待我的文件下载,然后触发加载事件。我想这对你也有用。

I used something like that in one of my projects. I was trying to download a file, and show message after download finished. This kind of thing didn't work in my experience:

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

Load event fires immediately after iframe is written in HTML not when iframe loads all of its contents. But this code worked:

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

This code waited for my file to download then fired load event. I think this could work for you too.

稀香 2024-12-06 02:54:17

我想当 i 框架内容在 IE 上完全加载时隐藏等待的微调 div,
我几乎尝试了 Stackoverflow.Com 中提到的所有解决方案,但都没有达到我想要的效果。

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

$(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";
         }
     });

希望这会有所帮助。

I wanted to hide the waiting spinner div when the i frame content is fully loaded on IE,
i tried literally every solution mentioned in Stackoverflow.Com, but with nothing worked as i wanted.

Then i had an idea, that when the i frame content is fully loaded, the $(Window ) load event might be fired.
And that exactly what happened.
So, i wrote this small script, and worked like magic:

$(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";
         }
     });

Hope this helps.

凉城凉梦凉人心 2024-12-06 02:54:17

你如何/在哪里触发 iframe 的加载?如果您检查这些解决方案,则会发现以下模式:

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

据报告有效。

类似的报告解决方案:

动态插入的 iframe 中的 jQuery .ready
IFRAME 加载完成时的 JavaScript 回调?

编辑:

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

How/where are you triggering the load of the iframe? If you check these solutions a pattern of:

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

is reported to work.

Similar reported solutions:

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

Edit:

Looks like Nick Spiers has the right answer, or at least it's part of your problem. Access issues on the iframe contents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文