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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:58:38+00:00 2026-05-14T03:58:38+00:00

In the answers to this question , we read that function f() {} defines

  • 0

In the answers to this question, we read that function f() {} defines the name locally, while [var] f = function() {} defines it globally. That makes perfect sense to me, but there’s some strange behavior that’s different between the two declarations.

I made an HTML page with the script

onload = function() {
    alert("hello");
}

and it worked as expected. When I changed it to

function onload() {
    alert("hello");
}

nothing happened. (Firefox still fired the event, but WebKit, Opera, and Internet Explorer didn’t, although frankly I’ve no idea which is correct.)

In both cases (in all browsers), I could verify that both window.onload and onload were set to the function. In both cases, the global object this is set to the window, and I no matter how I write the declaration, the window object is receiving the property just fine.

What’s going on here? Why does one declaration work differently from the other? Is this a quirk of the JavaScript language, the DOM, or the interaction between the two?

  • 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-14T03:58:39+00:00Added an answer on May 14, 2026 at 3:58 am

    This two snippets declares a function in the current scope, named “onload”. No binding is done.

    function onload() { ... }
    

    .

    var onload = function() { ... }
    

    This snippet assigns a function to a property/variable/field named “onload” on the current scope:

    onload = function() { ... }
    

    The reason why Firefox performed the binding and raised the onload event on the 1st snippet and the others didn’t might be because the Firefox chrome (its user interface) itself is written and automated using JavaScript – that’s why it’s so flexible and easy to write extensions on it. Somehow, when you declared the locally-scoped onload function that way, Firefox “replaced” the window‘s (most likely the local context at the time) implementation of onload (at that time, an empty function or undefined), when the other browsers correctly “sandboxed” the declaration into another scope (say, global or something).

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

Sidebar

Related Questions

I read the answers to this question and understand what the code var qq
I read all the answers that popped up when I started posting this question
Having read this question Immutable or not immutable? and reading answers to my previous
I've read numerous similar questions, but the answers simply didn't work. Running this function
I've read some of the previous question and answers about this issue but couldn't
Yes, I've read this question & answers: Passing an array by reference in C?
I read this question but the answer does not perform a refactoring of the
I read this question in stackoverflow. The excerpt answer provided by bbum is below:
I have read this question and the simple and clear answer but it's not
I've read all the answers on to this questions and none of the solutions

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.