in Education by
I made a simple page for testing: <!DOCTYPE html> // console.log("script run"); // document.addEventListener('DOMContentLoaded', function(event) { // alert("DOMContentLoaded event"); }); Test DOMContentLoaded And, sometimes (maybe once in 30 requests) DOMContentLoaded event is skipped. Perhaps this is due to incorrect loading of the page. But in log I see: "script run". I want make a duplicate DOMContentLoaded event function, and if DOMContentLoaded event is skipped, my function did the right job. I found this solutions: 1) // The basic check if(document.readyState === 'complete') { // good to go! } // Polling for the sake of my intern tests var interval = setInterval(function() { if(document.readyState === 'complete') { clearInterval(interval); done(); } }, 100); 2) HTMLDocument.prototype.ready = function () { return new Promise(function(resolve, reject) { if (document.readyState === 'complete') { resolve(document); } else { document.addEventListener('DOMContentLoaded', function() { resolve(document); }); } }); } document.ready().then(...); 3) document.addEventListener('readystatechange', function docStateChange(e) { if(e.target.readystate === 'complete') { e.target.removeEventListener('readystatechange', docStateChange); done(); } }); 4) // This is needed to prevent onreadystatechange being run twice var ready = false; document.onreadystatechange = function() { if (ready) { return; } // interactive = DOMContentLoaded & complete = window.load if (document.readyState == 'interactive' || document.readyState == 'complete') { ready = true; // init you code here } }; But which of the solutions is more correct? And what is the difference between these? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Worked for me: <!DOCTYPE html> // This is needed to prevent onreadystatechange being run twice var ready = false; document.onreadystatechange = function () { if (ready) { alert(document.getElementById('testing').getAttribute("class")); return; } // interactive = DOMContentLoaded & complete = window.load if (document.readyState === 'interactive' || document.readyState === 'complete') { ready = true; // init your code here } }; Test DOMContentLoaded

Run code snippetExpand snippet

Related questions

0 votes
    Some times due incorrect pattern of some lines of logs inside complete log files are skipped by logstash. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    What is the purpose of the event handlers in the JavaScript? (a) Adds innerHTML page to the code ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    Which is the opposite of the load event in JavaScript? (a) dontload (b) postload (c) preload (d) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 22, 2021 in Education by JackTerrance
0 votes
    Delete the branch starfeature in GitHub. Which channel would receive notification for this event? A. Stakeholders and ... . Stakeholders C. Starprojectteam D. None of the options...
asked Dec 23, 2022 in Technology by JackTerrance
0 votes
    Delete the branch starfeature in GitHub. Which channel would receive notification for this event? A. Stakeholders and ... . Stakeholders C. Starprojectteam D. None of the options...
asked Dec 16, 2022 in Education by JackTerrance
0 votes
    The Cold War ended with the event, ......... (a) Establishment of the United Nations (b) Disintegration ... Organisations (d) Cuban Missile Crisis Please answer the above question....
asked Aug 20, 2022 in Education by JackTerrance
0 votes
    ....... is an important event of the Cold War. (a) Fashoda Incident (b) Cuban Missile Crisis (c) Wall Street Crash (d) Red Menace Please answer the above question....
asked Aug 20, 2022 in Education by JackTerrance
0 votes
    Since I can't intercept action_down event for gridview, I would like to get (x,y) coordinate of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I am reading through the documentation of UIResponder class and I came across the responder chain. Now it's ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    How can I sort an event by id in DESC order? My all events are coming and by default they are ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    Have jquery dialog as closeOnescape as false. want to trigger an event based on esc key press how do ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    How to get callback in our app when user delete song from music app. Is there any solution? Thanks ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I found found several question's answers similar to this, I tried some of them, but no luck. May ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    How can I sort an event by id in DESC order? My all events are coming and by default they are ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    So the problem is i don't get get a value from textbox field on GridView RowUpdating event. Here ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
...