Link to home
Start Free TrialLog in
Avatar of ai-beng
ai-bengFlag for Germany

asked on

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
Avatar of ai-beng
ai-beng
Flag of Germany image

ASKER

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();
}
Avatar of ai-beng

ASKER

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...
ASKER CERTIFIED SOLUTION
Avatar of justinbillig
justinbillig

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial