10 examples of 'on dom ready' in JavaScript

Every line of 'on dom ready' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
7function domReady(callback) {
8 if (doc.readyState === "complete" || (doc.readyState !== "loading" && !doc.documentElement.doScroll))
9 setTimeout(() => callback && callback(), 0)
10 else {
11 let handler = () => {
12 doc.removeEventListener("DOMContentLoaded", handler, false)
13 win.removeEventListener("load", handler, false)
14 callback && callback()
15 }
16 doc.addEventListener("DOMContentLoaded", handler, false)
17 win.addEventListener("load", handler, false)
18 }
19}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

9export function domReady(callback) {
10 readyCallbacks.push(callback)
11}
42function domReady(callback) {
43 const doc = document;
44 const attach = 'addEventListener';
45
46 if (doc[attach]) {
47 doc[attach]('DOMContentLoaded', callback);
48 } else {
49 window.attachEvent('onload', callback);
50 }
51}
47function dom_ready()
48{
49 do_search();
50}
44function domReady(callback){
45 // TODO: support IE7-8
46 if(/e/.test(doc.readyState||'')){
47 // TODO: fix the issues with sync so this can be run immediately
48 setTimeout(callback, 200);
49 }else{
50 doc.addEventListener("DOMContentLoaded", callback);
51 }
52}
59function DOMREADY(handler) {
60 DOMREADY_CHECK();
61 (DOM_IS_READY) ? handler(): DOMREADY_QUEUE.push(handler);
62};
18function dom_ready(cb) {
19 function dom_ready_cb() {
20 dom_ready_cb = null_function;
21 cb();
22 }
23 document.addEventListener('DOMContentLoaded', dom_ready_cb, false);
24 var previous_onload = window.onload;
25 window.onload = function() {
26 dom_ready_cb();
27 previous_onload && previous_onload();
28 }
29}
59function domReady(fn){
60 if(document.addEventListener){
61 document.addEventListener('DOMContentLoaded',function(){
62 document.removeEventListener('DOMContentLoaded',this,false);
63 fn();
64 },false);
65 }else if(document.attachEvent){
66 document.attachEvent('onreadystatechange',function(){
67 if(document.readyState=='complete'){
68 document.detachEvent('onreadystatechange',this);
69 fn();
70 }
71 });
72 }
73}
20export function DOMReady():Promise<void> {
21 if(document.readyState === "loading") {
22 return new Promise(resolve =>
23 document.addEventListener("DOMContentLoaded", () => {resolve()})
24 );
25 } else {
26 return Promise.resolve();
27 }
28}
8export function _ready(cb) {
9 if (_isDomReady) {
10 return cb();
11 }
12
13 domready(() => {
14 _isDomReady = true;
15 setTimeout(cb, 10);
16 });
17}

Related snippets