title: Detecting DOM Ready State Within an iFrame (jQuery)
body: <p>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.</p>
  
  <p>Here is what I have already:</p>
  
  <pre><code>if (window.self !== window.top) {
      // is nested
      // DOM ready test here
          // execute code here
  } else {
      // is not nested
      $(document).ready(function() {
          // execute code here
      });
  }
  </code></pre>
  
  <p>I have already read <a href="https://stackoverflow.com/questions/205087/jquery-ready-in-a-dynamically-inserted-iframe">this post</a>, but I don't see the answer to my specific question.  I may be misunderstanding the solutions there, so feel free to correct me.</p>
  
  <hr>
  
  <p>EDIT:
  This seems to be a Fancybox issue.  This code:</p>
  
  <pre><code>alert(document.getElementById('username').className);
  document.getElementById('username').focus();
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>$(document).ready(function() {
      $('input.focus:last').focus();
  });
  </code></pre>
  
  <hr>
  
  <p>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:</p>
  
  <pre><code>'onComplete':function(){$('input.focus:last').focus();}
  </code></pre>