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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:13:11+00:00 2026-06-13T09:13:11+00:00

I have an unload and beforeunload event defined as: window.onunload = (function () {

  • 0

I have an unload and beforeunload event defined as:

   window.onunload =  (function () {
        alert("In unload); } ());
   window.beforeonunload = (function () {
        alert("In unload); } ());

When the page loads for the first time or after a postback (not before), the alert shows up. It shouldn’t. This happens in Firefox & Chrome but not in IE. Why? It seems IE has the correct behavior.

  • 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-13T09:13:12+00:00Added an answer on June 13, 2026 at 9:13 am

    Okay, so first of all, you’re missing quotes on the alerts, so it shouldn’t alert at all.

    To address your actual question, you’re invoking the function, not just declaring it.

    (function (){
        alert();
    }())
    

    (You also write beforeonunload. That is incorrect. It should be onbeforeunload) 

    That creates an anonymous function and invokes it immediately. They are generally called IIFE’s in JavaScript. To add an event handler, you have to pass a reference to a function. So the correct method here would be this:

    window.onunload =  function () {
         alert("In unload");
    };
    window.onbeforeunload = function () {
         alert("In unload");
    };​
    

    In your case, what is being assigned to the event is undefined, since your anonymous function doesn’t return anything. It would work perfectly if it returned a function.

    Chrome tends to block alerts onunload and onbeforeunload because those are generally spammy websites trying to prevent users from leaving. A console.log works perfectly, however. Check the demo. Open the console, click run, and since when you click run, the code is refreshed, the frame is reloaded, and you get the events being fired just before the page has been unloaded, and then loaded again.

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

Sidebar

Related Questions

I have the following javascript code that runs when a page is closed. $(window).unload(function
I have a jQuery function which monitors the 'beforeunload' status. So therefore every time
I have this inside my $(document).ready function: $(window).unload(function() { check_saved(); }); check_saved() just triggers
I'd like to have a javascript function called on page load and unload which
Do anyone have an idea about, how to register the window unload event in
I have tried for unload and beforeunload binding in following code, but none of
I have the following code: <html> <head> <title>D</title> <script type=text/javascript src=jQuery.js> <script type=text/javascript> $(window).unload(
I have a problem with this jQuery code: $(document).ready( function(){ alert($('#search').val()); var dummyInput =
I have a page that notifies the user about server updates, using window.webkitNotifications .
I have some problems with ASP.NET page cycle. I want to fire event when

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.