javascript: document | dom ready
2019-05-11 00:00:00
IN1
,
2023-04-10 14:16:30
IN1
jquery
// usage
$(document).ready(function() {
console.log('ready');
});
native javascript | vanilla js
short
function ready(f){/in/.test(document.readyState)?setTimeout('domReady('+f+')',9):f()}
// usage
ready(function(){
console.log('ready');
});
- taken from: https://stackoverflow.com/a/30319853/2487859
more complex
window.readyHandlers = [];
window.ready = function ready(handler) {window.readyHandlers.push(handler); handleState();};
window.handleState = function handleState () {if (['interactive', 'complete'].indexOf(document.readyState) > -1) {while(window.readyHandlers.length > 0) {(window.readyHandlers.shift())();}}};
document.onreadystatechange = window.handleState;
// usage
ready(function () {
console.log('ready');
});
- taken from: http://stackoverflow.com/a/13456810/2487859