Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Smallest x-browser DOM Ready, ever
function r(f){/in/(document.readyState)?setTimeout(r,9,f):f()}
@galambalazs

This comment has been minimized.

Show comment Hide comment
@galambalazs

galambalazs Feb 24, 2011

Doesn't seem to work: http://jsbin.com/ekeba4/ (it acts as window.onload)

Doesn't seem to work: http://jsbin.com/ekeba4/ (it acts as window.onload)

@dperini

This comment has been minimized.

Show comment Hide comment
@dperini

dperini Mar 9, 2011

@galambalazs,
"window.onload" works on any browser in the planet, this can only emulate "onload" in some browsers and fail with the rest.

It absolutely does not emulate a DOM Ready in any browsers. As an "onload" event alternative is not very performant and:

window.onload = f;

is also much shorter.

dperini commented Mar 9, 2011

@galambalazs,
"window.onload" works on any browser in the planet, this can only emulate "onload" in some browsers and fail with the rest.

It absolutely does not emulate a DOM Ready in any browsers. As an "onload" event alternative is not very performant and:

window.onload = f;

is also much shorter.

@galambalazs

This comment has been minimized.

Show comment Hide comment
@galambalazs

galambalazs Mar 9, 2011

@diego - Right, that's what I'm saying, cause the title is "DOM Ready" but this code fails to do that.

@diego - Right, that's what I'm saying, cause the title is "DOM Ready" but this code fails to do that.

@ded

This comment has been minimized.

Show comment Hide comment
@ded

ded Mar 9, 2011

yeah. i'm rethinking how this is gonna work now. be back with another implementation

Owner

ded commented Mar 9, 2011

yeah. i'm rethinking how this is gonna work now. be back with another implementation

@dperini

This comment has been minimized.

Show comment Hide comment
@dperini

dperini Mar 9, 2011

I have recently removed all the UA sniffing I was using in older versions and moved my stable cross-browser solution to GitHub here:

http://github.com/dperini/ContentLoaded

maybe you can have a look at it and compare the outcome. Currently only IFRAMEs do not participate in the optimization. Suggestions are welcome !

dperini commented Mar 9, 2011

I have recently removed all the UA sniffing I was using in older versions and moved my stable cross-browser solution to GitHub here:

http://github.com/dperini/ContentLoaded

maybe you can have a look at it and compare the outcome. Currently only IFRAMEs do not participate in the optimization. Suggestions are welcome !

@lukeasrodgers

This comment has been minimized.

Show comment Hide comment
@lukeasrodgers

lukeasrodgers Jul 15, 2011

Seems to work as described in Safari 5.0.5

Seems to work as described in Safari 5.0.5

@kajex

This comment has been minimized.

Show comment Hide comment
@kajex

kajex Feb 29, 2012

Very nice script to my small library.
What is the license?

kajex commented Feb 29, 2012

Very nice script to my small library.
What is the license?

@ded

This comment has been minimized.

Show comment Hide comment
@ded

ded Feb 29, 2012

use this: https://github.com/ded/domready - it's stable with a license too

Owner

ded commented Feb 29, 2012

use this: https://github.com/ded/domready - it's stable with a license too

@sp42

This comment has been minimized.

Show comment Hide comment
@sp42

sp42 Apr 24, 2013

I can not run this function with /in/.test....

sp42 commented Apr 24, 2013

I can not run this function with /in/.test....

@niechen

This comment has been minimized.

Show comment Hide comment
@niechen

niechen Feb 27, 2015

this check will fail if the document stays in "interactive" stage. use /in[^t]/ instead of /in/.

niechen commented Feb 27, 2015

this check will fail if the document stays in "interactive" stage. use /in[^t]/ instead of /in/.

@mjschutz

This comment has been minimized.

Show comment Hide comment
@mjschutz

mjschutz Nov 20, 2015

You have a syntax error on /in/(... must be /in/.test(... And the regex expression will return true in 'interactive ' it will only call f() when it's readyState is complete on some cases (file is on browser cache), not when it is loaded. So the best shot is:

function r(f){/(un|ing)/.test(document.readyState)?setTimeout(r,9,f):f()}

You have a syntax error on /in/(... must be /in/.test(... And the regex expression will return true in 'interactive ' it will only call f() when it's readyState is complete on some cases (file is on browser cache), not when it is loaded. So the best shot is:

function r(f){/(un|ing)/.test(document.readyState)?setTimeout(r,9,f):f()}

@ciscoheat

This comment has been minimized.

Show comment Hide comment
@ciscoheat

ciscoheat Feb 10, 2016

Thanks @mjschutz, your version works fine (at least on Chrome).

Thanks @mjschutz, your version works fine (at least on Chrome).

@ajaxboy

This comment has been minimized.

Show comment Hide comment
@ajaxboy

ajaxboy Aug 19, 2016

So far this is the most stable solution I have seen, I tried @dperini but does not appear to work if DOMContentLoaded is already called, by say, jQuery, then it won't work. This small piece of code in the other hand, even though I was concerned by the looping timer, seems to work fine, and doesn't seem to even show up in js profiler, so i think i am liking it. I also like the size.

ajaxboy commented Aug 19, 2016

So far this is the most stable solution I have seen, I tried @dperini but does not appear to work if DOMContentLoaded is already called, by say, jQuery, then it won't work. This small piece of code in the other hand, even though I was concerned by the looping timer, seems to work fine, and doesn't seem to even show up in js profiler, so i think i am liking it. I also like the size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment