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');
});

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');
});
This website uses Cookies to provide you with the best possible service. Please see our Privacy Policy for more information. Click the check box below to accept cookies. Then confirm with a click on "Save".