How to check if page load is complete

Hi,

I have a script that should run after the entire page load is complete.

Is there a way in Aurelia to figure out if the page load is completed, considering all the sub-modules are loaded ?

The script I am running is a call tracking script that would change targeted numbers on my page. Currently I have it running as the last step inside the config PostRender step and anything that loads after that is not observed by the script. Furthermore this script needs to be run on every page of my website.

Thanks in advance for your inputs.

Sounds hacky.

Why not change phone number literal 12345 to <call-tracking number="12345"></call-tracking>, then do your magic in custom element call-tracking?

A good thought but I am not what challenges I might come across since we are having other logic to set the phone numbers based on add campaign. If I am able to trigger the script after page loads, problem will be resolved since by then all other logic would have run…

I’ve searched for something similar and came up with this solution: Tracking render performance

Im hooking into the rendering process and presume that if there was no rendering process for 1 second the page is loaded, rendered and visible to the user. This time - the delay is my actual page load time. Except that a debugger corrupts this time it seem to work quite well.

There is also an event published by aurelia router you can try subscribe.

this.ea.subscribe('router:navigation:complete', () => {
  // this callback is fired every time a new page is loaded.

  // you may need a setTimeout here to wait for all sub-components. 
  // I did not test.
  setTimeout(() => {
    // your magic code
  });
});
2 Likes

There is the aurelia-composed event which is fired on document.

You can listen to this like:

document.addEventListener('aurelia-composed', () => {
    // finished loading?
});

I read about this here and it’s triggered here.

4 Likes

@fragsalat @huochunpeng @timfish
Thanks a lot for all your inputs. I haven’t got a chance to try any of these yet. I will definitely post it here when I do. Priorities got shifted to some other tasks as of now and I will need to circle back to this. Again thanks a ton for your valuable inputs :smiley:.