javascript Jquery ('#iframeid').load() 在 Iframe CONTENT (text, images) 加载之前执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7027161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery ('#iframeid').load() is executed before Iframe CONTENT (text, images) are loaded
提问by Abhischek
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?
我正在尝试访问 Iframe 的 Iframe 的数据。为了强制我的 javascript 函数等待,我使用 jqueries .load 函数。但是在 iframe 的内容完全加载之前,我的 javascript 代码仍然“有时”执行。有时我的意思是不同计算机的行为不同。在速度较慢的计算机上,它大部分时间都在等待速度较快的计算机,代码几乎总是被执行。所以我怀疑 Internet Explorer 8 会提前触发 onload 事件。是否有任何 jquery 函数可以等到 iframe 的所有元素都加载完毕?
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
所有 Iframe 都具有:相同的端口、协议和 src。非常感谢帮助
采纳答案by Nick Spiers
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.
Javascript 无法访问外部内容以在其完全加载时进行监控。如果您有权访问 iframe 内容,则可以添加对父窗口函数的调用以触发它。
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
});
回答by Masterhead
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:
Load 事件在 iframe 以 HTML 编写后立即触发,而不是在 iframe 加载其所有内容时触发。但是这段代码有效:
$('#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.
此代码等待我的文件下载然后触发加载事件。我认为这对你也有用。
回答by Nada N. Hantouli
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.
当 i 框架内容在 IE 上完全加载时,我想隐藏等待的微调 div,我实际上尝试了 Stackoverflow.Com 中提到的所有解决方案,但没有任何效果。
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:
然后我有了一个想法,当 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";
}
});
Hope this helps.
希望这可以帮助。
回答by numbers1311407
How/where are you triggering the load of the iframe? If you check these solutions a pattern of:
您如何/在哪里触发 iframe 的加载?如果您检查这些解决方案,则会出现以下模式:
$('#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?
当 IFRAME 完成加载时,jQuery .ready 在动态插入的 iframe
Javascript 回调中?
Edit:
编辑:
Looks like Nick Spiers has the right answer, or at least it's part of your problem. Access issues on the iframe contents.
看起来尼克斯皮尔斯有正确的答案,或者至少这是你问题的一部分。iframe 内容的访问问题。
回答by TheLibzter
I used this code to make sure the iFrame (identified by it's ID) was loaded before running my function:
我使用此代码来确保在运行我的函数之前加载了 iFrame(由它的 ID 标识):
$("#twitter-widget").load(myFunction);