Detecting DOM Ready State Within an iFrame jQuery

0 votes

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

Aug 2, 2022 in Web Development by gaurav
• 23,260 points
2,360 views

1 answer to this question.

0 votes

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.

answered Aug 2, 2022 by rajatha
• 7,680 points

Related Questions In Web Development

0 votes
0 answers

Scroll to an element with jQuery

I have this input element: <input type="text" class="textfield" ...READ MORE

Jul 1, 2022 in Web Development by gaurav
• 23,260 points
516 views
0 votes
0 answers

Page Reload/Refresh executes $(document).ready() on every page in jQuery Mobile

My jQuery mobile applications consists of multiple ...READ MORE

Jul 22, 2022 in Web Development by gaurav
• 23,260 points
945 views
0 votes
0 answers

How an I get all form elements (input, textarea & select) with jQuery?

Is there an easy way (without listing ...READ MORE

Jul 26, 2022 in Web Development by gaurav
• 23,260 points
2,843 views
0 votes
0 answers

jQuery Remain at the top of an open collapsible table row when clicked

In an ideal world i didn't want ...READ MORE

Jul 28, 2022 in Web Development by gaurav
• 23,260 points
446 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,520 points
1,350 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
15,881 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
23,389 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,840 points
3,341 views
0 votes
1 answer

How to change an element's title attribute using jQuery

You can change the title attribute with ...READ MORE

answered Jun 30, 2022 in Web Development by rajatha
• 7,680 points
4,721 views
0 votes
1 answer

How to change the jquery mobile flip switch state from code

I've examined the page you posted and ...READ MORE

answered Aug 1, 2022 in Web Development by rajatha
• 7,680 points
747 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP