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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:47:45+00:00 2026-06-09T16:47:45+00:00

I understand when passing a function pointer to an event handler you cannot invoke

  • 0

I understand when passing a function pointer to an event handler you cannot invoke the function block with parentheses or the return value of that function will be assigned to the event handler. I tried this and I’m confused on how it works?

window.onload = alert("Hello, World.");

I can see how this works:

window.onload = function () { alert("Hello, World!"); };

The literal function is not self-invoked leading to no return value and is only invoked once the onclick-event is invoked.

Edit 1: I don’t want to achieve anything with this. I just want to understand how window.onload = alert("Hello, World."); works perfectly and how window.onload = show_message("Hello, World."); doesn’t work?… Considering that show_message is actually a function.

Edit 2: Some user is claiming the onload event handler to work with parentheses on any function. I do not think this works like it should because the function is invoked ignoring the event handler and the return value of that function is assigned to the event handler. Most functions do not return anything so (undefined or null) will be assigned to the event handler.

Look at the code below:

var button = document.getElementById("button");

function test() {
    str = "works";
    console.log(str);
}

button.onclick = test;​

Assume there is a button element with the id of button assigned to it. This will only work if test is not invoked with parentheses (button.onclick = test();). Test will only run once and undefined will be assigned to onclick.

Edit 3: It looks like a return value is not assigned to an event handler if the function is invoked. It always writes out null to the console when I use console.log.

  • 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-09T16:47:46+00:00Added an answer on June 9, 2026 at 4:47 pm

    Nice question. Actually it does not work as you expect it to work. It’s just illusion that it works that way. When you run:

    window.onload = alert("Hello, World.");
    

    The right part of the statement is executed and alert is shown, but the window on load handler is not set up to some function, null will be assigned to it, since this is what alert returns.

    By the way even if you call the method with parentheses you can return function from it to assign to the event handler:

    var button = document.getElementById("button");
    
    function test() {
        str = "works";
        console.log(str);
        return function(){console.log('button clicked');}
    }
    
    button.onclick = test();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that predicates are delegate to function which return bool and take generic
As I understand it, when passing an object to a function that's larger than
I don't understand why passing a pointer to a function doesn't change the data
I understand, from MSDN, that ClassInitialize is to mark a method that will do
I understand passing a pointer, and returning a pointer: char * strcat ( char
I understand passing in a function to another function as a callback and having
I am currently learning bash programming and dont really understand why the passing argument
I understand this is not according to standard, however a partner is passing XML
My form is passing dates in the format 2 Apr 2012 I understand I
I understand that: '\n' // literally the backslash character followed by the character for

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.