0

Why it works without waiting for the page to be full loaded. Also, how can I run a function when the whole page is full loaded on Javascript?

enter image description here

Mahmut Bedir
  • 487
  • 1
  • 5
  • 23
  • this thread might help you https://stackoverflow.com/questions/2414750/difference-between-domcontentloaded-and-load-events – nmanikiran May 31 '20 at 19:13
  • No because the issue here is that `onload` is too _early_, not too _late_. – CherryDT May 31 '20 at 19:19
  • I edited my answer to include sample code that works on Twitter (note that it will be different for every problematic page though) – CherryDT May 31 '20 at 19:43

2 Answers2

2

It depends on your definition of "loaded".

The load event will fire...

[...]once a web page has completely loaded all content (including images, script files, CSS files, etc.). (Source)

This is the case here. All the images and script files and CSS files in the original HTML document have been loaded! However, the "loaded" state in case of Twitter is a state in which a script will later initiate the loading of new elements. That is outside the scope of the original page load.

If you want to wait for what you call "loaded", you'd have to ask yourself technically what point that is. I guess the answer would be something like "when element X is visible". In that case you can install a MutationObserver to listen on that element getting added/modified, after identifying what element you want to wait for by checking the DOM in your browser console. (The worse solution would be an interval that checks if that element exists yet.)

For Twitter, this works for me for example (watching for the first story to be loaded):

const observer = new MutationObserver(mutations => {
  if (mutations.some(mutation => {
    return Array.from(mutation.addedNodes).some(node => node.querySelector('article'))
  })) {
    setTimeout(() => { // Wait for all the updates of this frame to go through
      console.log('Page fully loaded!')
    }, 0)
    observer.disconnect()
  }
})

observer.observe(document, {
  attributes: false,
  childList: true,
  characterData: false,
  subtree: true
})
CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • Thanks for answer CherryDT, I try this method it looks good but until dont wait all tweets loading. I share the results image. https://imgur.com/3qLCOR9 – Mahmut Bedir May 31 '20 at 19:53
  • This is because the DOM did not rerender yet. You can add a `setTimeout` with `0` to fix that. I updated my answer. – CherryDT May 31 '20 at 20:53
-1

change your onload function syntax to any one of the following and try

window.addEventListener('load', (event) => {
 console.log('page is fully loaded');
});

with onload

window.onload = (event) => {
 console.log('page is fully loaded');
};
nmanikiran
  • 2,866
  • 15
  • 19
  • That wouldn't change at what time the event fires though. Your second suggestion is not even different from what the OP is already doing (only semantically)... – CherryDT May 31 '20 at 19:19
  • @nmanikiran Nothing changed. Again, sent an alert before the page was full – Mahmut Bedir May 31 '20 at 19:25