es6-docready
Version:
Document ready listener using plain javascript and ES6 module syntax
25 lines (17 loc) • 723 B
JavaScript
export default function docReady(callback) {
function completed() {
document.removeEventListener( "DOMContentLoaded", completed, false )
window.removeEventListener( "load", completed, false )
callback()
}
//Events.on(document, 'DOMContentLoaded', completed)
if ( document.readyState === "complete" ) {
// Handle it asynchronously to allow scripts the opportunity to delay ready
setTimeout( callback )
} else {
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", completed, false );
// A fallback to window.onload, that will always work
window.addEventListener( "load", completed, false );
}
}