Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:04:06+00:00 2026-06-13T23:04:06+00:00

Here is the circumstance: I have 2 pages: 1 x html page 1 x

  • 0

Here is the circumstance:
I have 2 pages:

  • 1 x html page
  • 1 x external Javascript

Now in the html page, there will be internal Javascript coding to allow the placement of the window.onload, and other page specific methods/functions.

But, in the external Javascript I want certain things to be done before the window.onload event is triggered. This is to allow customized components to be initialized first.

Is there a way to ensure initialization to occur in the external Javascript before the window.onload event is triggered?

The reason I have asked this, is to attempt to make reusable code (build once – use all over), to which the external script must check that it is in ‘order/check’ before the Javascript in the main html/jsp/asp/PHP page takes over. And also I am not looking for a solution in jQuery @_@

Here are some of the links on Stack Overflow I have browsed through for a solution:

  • Javascript – How to detect if document has loaded (IE 7/Firefox 3)
  • How to check if page has FULLY loaded(scripts and all)?
  • Execute Javascript When Page Has Fully Loaded

Can someone help or direct me to a solution, your help will be muchness of greatness appreciated.


[updated response – 19 November 2012]

Hi all, thanks for you advice and suggested solutions, they have all been useful in the search and testing for a viable solution.
Though I feel that I am not 100% satisfied with my own results, I know your advice and help has moved me closer to a solution, and may indeed aid others in a similar situation.

Here is what I have come up with:

test_page.html

<html>
<head>
<title></title>
<script type="text/javascript" src="loader.js"></script>
<script type="text/javascript" src="test_script_1.js"></script>
<script type="text/javascript" src="test_script_2.js"></script>
<script type="text/javascript">
window.onload = function() {
    document.getElementById("div_1").innerHTML = "window.onload complete!";
}
</script>

<style type="text/css">
div {
    border:thin solid #000000;
    width:500px;
}
</head>
<body>
    <div id="div_1"></div>

    <br/><br/>

    <div id="div_2"></div>

    <br/><br/>

    <div id="div_3"></div>
</body>
</html>

loader.js

var Loader = {
    methods_arr : [],

    init_Loader : new function() {
        document.onreadystatechange = function(e) {
            if (document.readyState == "complete") {
                for (var i = 0; i < Loader.methods_arr.length; i++) {
                    Loader.method_arr[i]();
                }
            }
        }
    },

    load : function(method) {
        Loader.methods_arr.push(method);
    }
}

test_script_1.js

Loader.load(function(){initTestScript1();});

function initTestScript1() {
    document.getElementById("div_1").innerHTML = "Test Script 1 Initialized!";
}

test_script_2.js

Loader.load(function(){initTestScript2();});

function initTestScript2() {
    document.getElementById("div_2").innerHTML = "Test Script 2 Initialized!";
}

This will ensure that scripts are invoked before invocation of the window.onload event handler, but also ensuring that the document is rendered first.

What do you think of this possible solution?

Thanking you all again for the aid and help 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:04:07+00:00Added an answer on June 13, 2026 at 11:04 pm

    Basically, you’re looking for this:

    document.onreadystatechange = function(e)
    {
        if (document.readyState === 'complete')
        {
            //dom is ready, window.onload fires later
        }
    };
    window.onload = function(e)
    {
        //document.readyState will be complete, it's one of the requirements for the window.onload event to be fired
        //do stuff for when everything is loaded
    };
    

    see MDN for more details.

    Do keep in mind that the DOM might be loaded here, but that doesn’t mean that the external js file has been loaded, so you might not have access to all the functions/objects that are defined in that script. If you want to check for that, you’ll have to use window.onload, to ensure that all external resources have been loaded, too.

    So, basically, in your external script, you’ll be needing 2 event handlers: one for the readystatechange, which does what you need to be done on DOMready, and a window.onload, which will, by definition, be fired after the document is ready. (this checks if the page is fully loaded).
    Just so you know, in IE<9 window.onload causes a memory leak (because the DOM and the JScript engine are two separate entities, the window object never gets unloaded fully, and the listener isn’t GC’ed). There is a way to fix this, which I’ve posted here, it’s quite verbose, though, but just so you know…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
I realize there have been a few other questions on this topic, and the
Here's the scenario: I've got a regular hyperlink on my .ascx. (no I have
In some circumstances, I have to pass a request to a Wicket page to
I have an ASP .Net page with a radio button group and a text
I'm trying to do a raw POST to an internal page from a unit
Some people have mentioned (e.g. here on StackOverflow ) the tofu scale when talking
sorry for re-asking, but i have new circumstances. here was my question .net -
In my app I have an advanced search page which uses a tableview with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.