How to know if a page is loaded (Ie my script was loaded after the onload event)

Hi,

I provide a script that has to start when the DOM is ready (OnLoad or OnDOMContentLoaded), but it is only executed in 3rd party webpages, so i don't know where it was put in the page or if it was even loaded by another js.

So I use an EventListener for OnLoad/OnDOMContentLoaded, but how do I know, if OnLoad has already triggered?

In Browsers which support document.readyState this is not a problem, but what about the others?

Thx in advance
ai-bengAsked:
Who is Participating?
 
justinbilligConnect With a Mentor Commented:
Unfourtunatly there is not going to be a solution that will catch every possible situation. If you can't enforce where in the page the user puts the script then there is no way to guarantee any situation.
0
 
ai-bengAuthor Commented:
My solution is to use this function in Firefox (all other supported browsers support document.readyState ):

function onReady( pCallback )
{
    try
    {
        var oElem = document.createElement('a');
        document.body.appendChild( oElem );
        document.body.removeChild( oElem );
    }
    catch( oException )
    {
        setTimeout( function(){ onReady( pCallback ); }, 0 );
        return;
    }
   
    pCallback();
}
0
 
ai-bengAuthor Commented:
But my onReady-function is also not really acceptable, because it only means that the DOM is writeable, not that the complete page is loaded...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.