Skip to content

Instantly share code, notes, and snippets.

@koreapyj
Last active September 29, 2017 02:04
Show Gist options
  • Save koreapyj/c7c0b8c7e5b4bff929707f939cf3da51 to your computer and use it in GitHub Desktop.
Save koreapyj/c7c0b8c7e5b4bff929707f939cf3da51 to your computer and use it in GitHub Desktop.
DOM을 다룰 수 있게 될 때 이벤트를 발생시킵니다. (cross browser)
function onDOMReady(listener) {
if(document.readyState) switch(document.readyState) {
case 'complete':
listener();
break;
case 'interactive':
/* "interactive" in IE 9 is quite different - we can't handle DOM elements at this time */
if(!(__MSIE__ = navigator.appVersion.match(/MSIE ([0-9]+\.[0-9]+)/)) || __MSIE__[1] > 9) {
listener();
break;
}
default:
document.addEventListener('load', listener);
document.addEventListener('DOMContentLoaded', listener);
}
else {
// browser seems like way too old (ie < 8.0) - time to give up
throw 'YourMother: Too old browser!!!';
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment