最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detecting DOM Ready State Within an iFrame (jQuery) - Stack Overflow

programmeradmin3浏览0评论

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.

Here is what I have already:

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.


EDIT: This seems to be a Fancybox issue. This code:

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

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

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

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.

Here is what I have already:

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.


EDIT: This seems to be a Fancybox issue. This code:

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

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

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();}
Share Improve this question edited May 23, 2017 at 10:33 CommunityBot 11 silver badge asked Feb 1, 2010 at 17:18 SonnySonny 8,3367 gold badges65 silver badges137 bronze badges 2
  • If you use the ready function also when it's nested it should work... – mck89 Commented Feb 1, 2010 at 17:22
  • $(document).ready(function() is not working in my iframe. This specific iframe is dynamically generated by Fancybox. I would like a solution that isn't specific to Fancybox though. – Sonny Commented Feb 1, 2010 at 17:26
Add a ment  | 

1 Answer 1

Reset to default 4

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.

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>

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.

发布评论

评论列表(0)

  1. 暂无评论