Skip to content

Instantly share code, notes, and snippets.

@yocontra
Created April 24, 2012 07:55
Show Gist options
  • Save yocontra/2477661 to your computer and use it in GitHub Desktop.
Save yocontra/2477661 to your computer and use it in GitHub Desktop.
Tiny DOMReady javascript/coffee-script
# DOM ready
window.ready ?= (fn) ->
fire = ->
unless window.ready.fired
window.ready.fired = true
fn()
return fire() if document.readyState is "complete"
# Mozilla, Opera, WebKit
if document.addEventListener
document.addEventListener "DOMContentLoaded", fire, false
window.addEventListener "load", fire, false
# IE
else if document.attachEvent
check = ->
try
document.documentElement.doScroll "left"
catch e
setTimeout check, 1
return
fire()
document.attachEvent "onreadystatechange", fire
window.attachEvent "onload", fire
check() if document.documentElement and document.documentElement.doScroll and !window.frameElement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment