在iFrame(jQuery)中检测DOM就绪状态 [英] Detecting DOM Ready State Within an iFrame (jQuery)

查看:113
本文介绍了在iFrame(jQuery)中检测DOM就绪状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本正在一个可能嵌套在iframe中的文档中运行。我已经整理了检测嵌套的问题,但是我不知道如何在iframe中检测到DOM的状态。

I have a script being run in a document that may or may not be nested in an iframe. I have sorted out the issue of detecting the nested, but I can't figure out how to detect the ready state of the DOM when it is an iframe.

这里是我已经有:

if (window.self !== window.top) {
    // is nested
    // DOM ready test here
        // execute code here
} else {
    // is not nested
    $(document).ready(function() {
        // execute code here
    });
}

我已经读过这篇文章,但是我没有看到我的具体问题的答案。我可能误解了那里的解决方案,所以请随时更正我。

I have already read this post, but I don't see the answer to my specific question. I may be misunderstanding the solutions there, so feel free to correct me.

编辑:
这似乎是成为Fancybox问题。这段代码:

This seems to be a Fancybox issue. This code:

alert(document.getElementById('username').className);
document.getElementById('username').focus();

我看到具有正确值的警报,但是表单元素没有收到焦点。这告诉我,检测DOM的就绪状态不是问题。我试图使用的原始脚本是:

I see an alert with the correct value, but the form element does not receive the focus. This tells me that it's not an issue with detecting the DOM's ready state. The original script I was trying to use was this:

$(document).ready(function() {
    $('input.focus:last').focus();
});






编辑2 /解决方案:
I不得不诉诸于Fancybox的具体解决方案,因为它似乎是一个Fancybox的具体问题。我将这行添加到这些Fancybox iframed表单的配置中:


EDIT 2/SOLUTION: I had to resort to a Fancybox specific solution because it appears to be a Fancybox specific issue. I added this line to the configuration for these Fancybox iframed forms:

'onComplete':function(){$('input.focus:last').focus();}


推荐答案

您可以在iframe BODY标签的末尾放置一个SCRIPT标签。

然后在其中添加您的代码,在加载iframe正文时将执行。

You can place a SCRIPT tag at the end of the iframe BODY tag.
Then add your code there, it will be executed when the iframe body is loaded.

您可以运行iframe的本地JS代码,也可以调用父类中的对象,如:

You can either run a local JS code of the iframe, or call an object in the parent like:

<body>
  ...
  <script>
    parent.myObj.iframeLoaded(document.body);
  </script>
</body>

根据我的经验,IE中的onload事件不可靠。

即:它不会等待在触发之前加载iframe中的JS。

To my experience, the onload event in IE is not reliable.
i.e: it won't wait to the JS inside the iframe to be loaded before firing.

这篇关于在iFrame(jQuery)中检测DOM就绪状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆