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 6754185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:14:59+00:00 2026-05-26T13:14:59+00:00

I have examined Dean Edward’s onload post and have a question: what’s wrong with

  • 0

I have examined Dean Edward’s onload post and have a question: what’s wrong with this onload attaching method? Where and why can it fail? And when is it safe to use this simpler function?

function addLoadEvent(func){
    var oldonload = window.onload;
    if(typeof window.onload != 'function'){
        window.onload = func;
    }else{
        window.onload = function(){
            oldonload();
            func();
        }
    }
}
  • 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-05-26T13:15:00+00:00Added an answer on May 26, 2026 at 1:15 pm

    The point of Dean’s post isn’t about how to hook the window.load event, but rather how to hook a useful event prior to window.load. window.load happens very, very late in the page load cycle (after all resources, including all images, are fully loaded). If your page has any significant resources and you wait until window.load to hook up your event handlers, your users are likely to start doing things before your handlers are hooked up.

    Note that Dean’s article was written in 2006. The modern practice is to simply put your script just prior to the closing </body> tag. As long as your script is after any elements it refers to, the elements will be accessible to you. That way, you hook things up very early on indeed.

    Example (live copy):

    HTML:

    <p>This page has a big image on it, to help demonstrate
      <code>window.load</code>'s very late arrival.
      Note that this paragraph started out black, but was
      turned green <em>immediately</em> on page load.
      This demonstrates that the paragraph was available
      to the code at the end of the page.
    </p>
    <img src="http://apod.nasa.gov/apod/image/1110/sh2136s_block900.jpg" style="position: absolute; top: 20em;">
    

    JavaScript (just before </body> end tag):

    (function() {
      var first;
    
      function hookLoad(handler) {
        if (window.addEventListener) {
          window.addEventListener("load", handler, false);
        }
        else if (window.attachEvent) {
          window.attachEvent("onload", handler);
        }
      }
    
      display("Script running at end of page; turning paragraph green.");
      document.getElementsByTagName('p')[0].style.color = "green";
    
      hookLoad(function() {
        display("First load handler");
      });
    
      hookLoad(function() {
        display("Second load handler");
      });
    
      function display(msg) {
        var p = document.createElement('p'),
            now = new Date().getTime();
        msg = now + ": " + msg;
        if (first) {
          msg += " (delayed by " + (now - first) + "ms)";
        }
        else {
          first = now;
        }
        p.innerHTML = msg;
        document.body.appendChild(p);
      }
    })();
    

    If you can’t do that (put the script at the end of the body element) for some reason, then tricks like Dean’s start being useful, but I’d strongly recommend against DIY “DOM loaded” handling; instead, use a good, well-maintained library like jQuery, Prototype, YUI, or any of several others and use their “ready” event. By leveraging the ongoing work put into those libraries, you can instead focus on what you’re trying to do with your own page/app.


    References:

    • YUI’s best practices for speeding up your website
    • Google on when DOM elements are accessible (and why their library doesn’t have a “DOM ready” event)

    [Re window.load specifically: You don’t have to resort to your trick, just use addEventListener (if the browser supports it, all do except older IE versions) or attachEvent (older IE). See hookLoad in the example above. Those allow multiple handlers for an event.]

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

Sidebar

Related Questions

I have a routine that examines thousands of records looking for discrepancies. This can
I got an email today saying: In every case that we have examined, this
I have examined Zend Framework: Zend_Validate_Db_RecordExists and Zend_Validate_Db_NoRecordExists, and observed that you can check
Have a look at this picture alt text http://www.abbeylegal.com/downloads/2009-04-01/web%20part%20top%20line.jpg Does anyone know what css
Using MVC 2 I have been trying to make this record store project. Creating
I have spent 5 hours on this, and I've already been up 30 hours
Let's say I have this class: public abstract class CustomerCollectionBase : Collection<Customer>{} One of
I have a path problem with UIImage.FromFile() method. My solution folder has 3 projects
I have some C# code that looks something like this System.Data.DataRow row; var table
Few days ago I have asked a question about 1,2 and 3. degree connections.

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.