Skip to content

Instantly share code, notes, and snippets.

@ded
Created February 24, 2011 08:46
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save ded/841930 to your computer and use it in GitHub Desktop.
Save ded/841930 to your computer and use it in GitHub Desktop.
Smallest x-browser DOM Ready, ever
function r(f){/in/(document.readyState)?setTimeout(r,9,f):f()}
@sp42
Copy link

sp42 commented Apr 24, 2013

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

@niechen
Copy link

niechen commented Feb 27, 2015

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

@mjschutz
Copy link

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
Copy link

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

@ajaxboy
Copy link

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