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

Posted on 2008-11-10
Last Modified: 2012-05-05

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
Question by:ai-beng

    Author Comment

    My solution is to use this function in Firefox (all other supported browsers support document.readyState ):

    function onReady( pCallback )
            var oElem = document.createElement('a');
            document.body.appendChild( oElem );
            document.body.removeChild( oElem );
        catch( oException )
            setTimeout( function(){ onReady( pCallback ); }, 0 );

    Author Comment

    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...
    LVL 15

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 Mobile App Development with PhoneGap

    PhoneGap can help you leverage your already existing HTML5, JavaScript, and CSS skills in order to create and deploy cross-platform mobile apps.This program comprehensively covers HTML5 mobile app development from top to bottom.

    Suggested Solutions

    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved." Ralph Waldo Emerson Introduction: One of the wonderful things about the web is that it makes it s…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now