jQuery 检查 iframe 是否加载到 $(document).ready 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17045645/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:19:06  来源:igfitidea点击:

Check if iframe is loaded inside $(document).ready

javascriptjquery

提问by Uros K

I want to check if iframe is loaded with the following code:

我想检查 iframe 是否加载了以下代码:

$(document).ready(function() {
    jQuery('#iframeID').ready(somefunction);
}

It seems that 'somefunction' is called before iframe is loaded (the iframe is empty - just empty html-head-body).

似乎在加载 iframe 之前调用了“somefunction”(iframe 是空的 - 只是空的 html-head-body)。

Any idea why this happens?

知道为什么会这样吗?

Thank you.

谢谢你。

回答by Praveen Kumar Purushothaman

Try this instead.

试试这个。

$('#iframeID').load(function() {
    callback(this);
});

While dealing with iFrames, it is good enough to use load()event instead of $(document).ready()event.

在处理 iFrame 时,使用load()事件代替$(document).ready()事件就足够了。

回答by pandavenger

This is because you're checking if the iFrame is ready, not the document inside.

这是因为您正在检查 iFrame 是否准备好,而不是里面的文档。

$(document.getElementById('myframe').contentWindow.document).ready(someFunction);

should do the trick.

应该做的伎俩。

回答by Jayram

I have tried:

我试过了:

$("#frameName").ready(function() {
    // Write you frame on load javascript code here
} );

and it did not work for me.

它对我不起作用。

this did:

这做到了:

$("#frameName").load( function() {
     //code goes here
} );

Even though the event does not fire as quickly - it waits until images and css have loaded also.

即使事件没有那么快触发 - 它也会等到图像和 css 也加载完毕。