Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
agree with the comment about brevity
Source Link
oriadam
  • 8.8k
  • 3
  • 56
  • 49

You can check the document's readyState value and this way tell if the event was fired or not. Here's the code to run a function named start() when the document has finished parsing:

if (/comp|inter|loadedcomplete|interactive|loaded/.test(document.readyState)) { // In case the document has finished parsing, document's readyState will // be one of "complete", "interactive" or (non-standard) "loaded". // The regexp above looks for all three states. A more readable regexp // would be /complete|interactive|loaded/ start(); } else { // The document is not ready yet, so wait for the DOMContentLoaded event document.addEventListener('DOMContentLoaded', start, false); } 

Notice that the code above detects when the document has finished parsing. Beware that's not the same as detecting if DOMContentLoaded was fired (which happens immediately after interactive), but it serves the same practical purpose, i.e., it tells you that the document has finished loading and has been parsed, but sub-resources such as images, stylesheets and frames are still loading (source).

You can check the document's readyState value and this way tell if the event was fired or not. Here's the code to run a function named start() when the document has finished parsing:

if (/comp|inter|loaded/.test(document.readyState)) { // In case the document has finished parsing, document's readyState will // be one of "complete", "interactive" or (non-standard) "loaded". // The regexp above looks for all three states. A more readable regexp // would be /complete|interactive|loaded/ start(); } else { // The document is not ready yet, so wait for the DOMContentLoaded event document.addEventListener('DOMContentLoaded', start, false); } 

Notice that the code above detects when the document has finished parsing. Beware that's not the same as detecting if DOMContentLoaded was fired (which happens immediately after interactive), but it serves the same practical purpose, i.e., it tells you that the document has finished loading and has been parsed, but sub-resources such as images, stylesheets and frames are still loading (source).

You can check the document's readyState value and this way tell if the event was fired or not. Here's the code to run a function named start() when the document has finished parsing:

if (/complete|interactive|loaded/.test(document.readyState)) { // In case the document has finished parsing, document's readyState will // be one of "complete", "interactive" or (non-standard) "loaded". start(); } else { // The document is not ready yet, so wait for the DOMContentLoaded event document.addEventListener('DOMContentLoaded', start, false); } 

Notice that the code above detects when the document has finished parsing. Beware that's not the same as detecting if DOMContentLoaded was fired (which happens immediately after interactive), but it serves the same practical purpose, i.e., it tells you that the document has finished loading and has been parsed, but sub-resources such as images, stylesheets and frames are still loading (source).

added 436 characters in body
Source Link
Lucio Paiva
  • 21.1k
  • 11
  • 88
  • 110

The document.readyState is changed when this event is fired. So youYou can check the document's readyState value and this way tell if the event was fired or not. Here's an Here's the code to run a function named start() when the document is ready for ithas finished parsing:

if (/comp|inter|loaded/.test(document.readyState)){ // In case DOMContentLoaded was already fired, the document readyState will be one of "complete" or "interactive" or (nonstandard) "loaded". // The regexp above looks for all three states. A more readable regexp would be /complete|interactive|loaded/ start(); }else{ // In case DOMContentLoaded was not yet fired, use it to run the "start" function when document is read for it. document.addEventListener('DOMContentLoaded', start, false); } 
if (/comp|inter|loaded/.test(document.readyState)) { // In case the document has finished parsing, document's readyState will // be one of "complete", "interactive" or (non-standard) "loaded". // The regexp above looks for all three states. A more readable regexp // would be /complete|interactive|loaded/ start(); } else { // The document is not ready yet, so wait for the DOMContentLoaded event document.addEventListener('DOMContentLoaded', start, false); } 

Notice that the code above detects when the document has finished parsing. Beware that's not the same as detecting if DOMContentLoaded was fired (which happens immediately after interactive), but it serves the same practical purpose, i.e., it tells you that the document has finished loading and has been parsed, but sub-resources such as images, stylesheets and frames are still loading (source).

The document.readyState is changed when this event is fired. So you can check the readyState value and this way tell if the event was fired or not. Here's an code to run start() when document is ready for it:

if (/comp|inter|loaded/.test(document.readyState)){ // In case DOMContentLoaded was already fired, the document readyState will be one of "complete" or "interactive" or (nonstandard) "loaded". // The regexp above looks for all three states. A more readable regexp would be /complete|interactive|loaded/ start(); }else{ // In case DOMContentLoaded was not yet fired, use it to run the "start" function when document is read for it. document.addEventListener('DOMContentLoaded', start, false); } 

You can check the document's readyState value and this way tell if the event was fired or not. Here's the code to run a function named start() when the document has finished parsing:

if (/comp|inter|loaded/.test(document.readyState)) { // In case the document has finished parsing, document's readyState will // be one of "complete", "interactive" or (non-standard) "loaded". // The regexp above looks for all three states. A more readable regexp // would be /complete|interactive|loaded/ start(); } else { // The document is not ready yet, so wait for the DOMContentLoaded event document.addEventListener('DOMContentLoaded', start, false); } 

Notice that the code above detects when the document has finished parsing. Beware that's not the same as detecting if DOMContentLoaded was fired (which happens immediately after interactive), but it serves the same practical purpose, i.e., it tells you that the document has finished loading and has been parsed, but sub-resources such as images, stylesheets and frames are still loading (source).

added 368 characters in body
Source Link
oriadam
  • 8.8k
  • 3
  • 56
  • 49

SafelyThe document.readyState is changed when this event is fired. So you can check the readyState value and this way tell if the event was fired or not. Here's an code to run start() when document is ready for it:

if (/comp|inter|loaded/.test(document.readyState)){ // In case DOMContentLoaded was already fired, the document readyState will be one of "complete" or "interactive" or (nonstandard) "loaded". // The regexp above looks for all three states. A more readable regexp would be /complete|interactive|loaded/ start(); }else{ // In case DOMContentLoaded was not yet fired, use it to run the "start" function when document is read for it. document.addEventListener('DOMContentLoaded', start, false); } 

Safely run start() when document is ready for it:

if (/comp|inter|loaded/.test(document.readyState)){ // In case DOMContentLoaded was already fired, the document readyState will be one of "complete" or "interactive" or (nonstandard) "loaded". // The regexp above looks for all three states. A more readable regexp would be /complete|interactive|loaded/ start(); }else{ // In case DOMContentLoaded was not yet fired, use it to run the "start" function when document is read for it. document.addEventListener('DOMContentLoaded', start, false); } 

The document.readyState is changed when this event is fired. So you can check the readyState value and this way tell if the event was fired or not. Here's an code to run start() when document is ready for it:

if (/comp|inter|loaded/.test(document.readyState)){ // In case DOMContentLoaded was already fired, the document readyState will be one of "complete" or "interactive" or (nonstandard) "loaded". // The regexp above looks for all three states. A more readable regexp would be /complete|interactive|loaded/ start(); }else{ // In case DOMContentLoaded was not yet fired, use it to run the "start" function when document is read for it. document.addEventListener('DOMContentLoaded', start, false); } 
added 368 characters in body
Source Link
oriadam
  • 8.8k
  • 3
  • 56
  • 49
Loading
Source Link
oriadam
  • 8.8k
  • 3
  • 56
  • 49
Loading