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

  • Home
  • SEARCH
  • 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 537887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:58:27+00:00 2026-05-13T09:58:27+00:00

I have one paragraph of javascript code. And i don’t understand it very well.

  • 0

I have one paragraph of javascript code. And i don’t understand it very well.
Can you expain it line by line for me? Thanks a lot.

 function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

And here is what i’m thinking:

 function addLoadEvent(func) { //define a function with a parameter 'func'
  var oldonload = window.onload; //assign window.onload event to variable oldonload
  if (typeof window.onload != 'function') { //if window.onload is not a function, then...
    window.onload = func; //assign 'func' to window.onload event. what does func mean?
  } else {             //if window.onlad is a function
    window.onload = function() {  //don't understand
      oldonload();  //call function oldonload()
      func();       //call function func()
    }
  }
}

Confusions:

window.onload is already an event, and why do we use typeof?

function addLoadEvent(func) , window.onload = func, func(). What’s the difference among these funcs?

I’m sorry for posting a novice problem. But thanks to anyone who gives me some guidance?

Edit:

This is improved original code by Simon Willison.

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        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-13T09:58:28+00:00Added an answer on May 13, 2026 at 9:58 am

    Its very simple.

    You check if there is already an onload function registered.

    if there isnt one,
    1. assign ‘func’ the function you pass in to the onload

    if there is one,
    create a new onload function, that will:

    1. call the old onload function
    2. call your ‘func’ that you pass in

    and:

    window.onload is already an event, and
    why do we use typeof?

    window.onload can be a function, but if one isnt set, it will be ‘undefined’ we need to check its type to see what it is.

    function addLoadEvent(func) ,
    window.onload = func, func(). What’s
    the difference among these funcs?

    functions are a variable in javascript. so you can refer to the function as

    func

    you can call it as

    func()

    in your case: function addLoadEvent(func) is the current function defintion. it takes one param, and that param should be a function

    window.onload = func
    assigns the function you passed in to the onload event

    func()

    calls the function you passed in

    here is the line by line correction:

    function addLoadEvent(func) { //define a new function called addLoadEvent which takes in one param which should be function
      var oldonload = window.onload; //assign window.onload event to variable oldonload
      if (typeof window.onload != 'function') { //if window.onload is not a function,  and thus has never been defined before elsewhere
        window.onload = func; //assign 'func' to window.onload event. set the function you passed in as the onload function
      } else {             //if window.onlad is a function - thus already defined, we dont want to overwrite it so we will..
        window.onload = function() {  //define a new onload function that does the following:
          oldonload();  //do whatever the old onload function did
          func();       //and then do whatever your new passed in function does
        }
      }
    }
    
    //then call it like this: 
    addLoadEvent(function() {
      alert("hi there");
    });
    addLoadEvent(function() {
      alert("this will be alerted after hi there");
    });
    //or like this:
    var fn = function(){
     alert("this will be the last thing alerted");
    };
    addLoadEvent(fn);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 392k
  • Answers 392k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Memcache IO happens on either getDelayed or fetchAll. getDelayed basically… May 15, 2026 at 1:44 am
  • Editorial Team
    Editorial Team added an answer You need to modify the generation so that it builds… May 15, 2026 at 1:44 am
  • Editorial Team
    Editorial Team added an answer It's not always a good approach to generate the DB… May 15, 2026 at 1:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.