10 examples of 'vanilla js document ready' in JavaScript

Every line of 'vanilla js document 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
75export function documentReady(cb) {
76
77 if (stateIsInteractive() || stateIsComplete()) {
78 cb();
79 }
80
81 function loadedHandler() {
82 removeHandler(doc, 'DOMContentLoaded', loadedHandler);
83 cb();
84 }
85
86 addHandler(doc, 'DOMContentLoaded', loadedHandler);
87}
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

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}
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}
9export function domReady(callback) {
10 readyCallbacks.push(callback)
11}
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}
69function documentReady() {
70 // Get initial tracking status
71 matomoOptOutStatus();
72
73 // Add listener for the "do track" button.
74 var doTrackBtn = document.querySelector('.MatomoOptout-button--track');
75 doTrackBtn.addEventListener('click', function (event) {
76 event.preventDefault();
77 event.stopPropagation();
78
79 matomoOptOutTrack();
80 });
81
82 // Add listener for the "do not track" button.
83 var doBlockBtn = document.querySelector('.MatomoOptout-button--block');
84 doBlockBtn.addEventListener('click', function (event) {
85 event.preventDefault();
86 event.stopPropagation();
87
88 matomoOptOutStatus();
89 if (matomoOptOutIsTracked === true) {
90 matomoOptOutBlock();
91 }
92 });
93}
42function bootstrapDomReady() {
43 // bootstrap after document is ready
44 return document.addEventListener('DOMContentLoaded', main);
45}
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}
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}
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