jrburke/requirejs — Issue #168: domReady.js tweaks
Author: jdalton
Created: 2012-01-17 15:18:29+00:00
State: open

Accessing `self.top` [may cause polling to take ~9-15 seconds](http://bugs.jquery.com/ticket/4787) if mixed with setting `document.domain`.

To avoid that you can use `window.frameElement` instead, but it has to be wrapped in a try-catch to avoid errors that are thrown if it's not accessible.

jQuery does this like:

~~~ js
var toplevel = fase;
try {
toplevel = window.frameElement == null;
} catch(e) {}
~~~

Also to address the concern of:

~~~ js
//http://bugs.dojotoolkit.org/ticket/11106,
//In IE HTML Application (HTA), such as in a selenium test,
//javascript in the iframe can't see anything outside
//of it, so self===self.top is true, but the iframe is
//not the top window and doScroll will be available
//before document.body is set. Test document.body
//before trying the doScroll trick.
if (document.body) {
~~~

Instead of the `document.body` check which can be available too soon due to loading techniques that use PHP's ` flush()`, like [progressive rendering](http://www.phpied.com/progressive-rendering-via-multiple-flushes/), you can check for the non `null` value of the `window.external` property as HTA's [don't have that](http://msdn.microsoft.com/en-us/library/ms536496\(v=vs.85\).aspx#Compatibility).

*(`window.external` is `null` for chromeless IEs, like platform preview releases too)*