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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:27:25+00:00 2026-05-23T15:27:25+00:00

I am displaying a warning message if user try to close window without saving

  • 0

I am displaying a warning message if user try to close window without saving the form.

window.onbeforeunload = askConfirm;

    function askConfirm()
    {
        // needToConfirm is set to true if any changes are there in the form
        if (needToConfirm)
        {
            return "Your unsaved data will be lost.";
        }       
    } 

    function call_this_if_user_clicks_on_cancel()
    {
       // Bla bla bla
       // After this user should remain on the same page.
    }  

Now I want to call another function when user clicks on Okay. And rest of the functionality should remain same.
So how can I collect onbeforeunload return value and call another function ?

SOLUTION:

    needToConfirm = false; 
    window.onbeforeunload = askConfirm;
    window.onunload = unloadPage;
    isDelete = true;

    function unloadPage()
    {
        if(isDelete)
        {
           call_this_if_user_clicks_on_cancel() 
        }   
    }

    function askConfirm()
    {
        alert("onbeforeunload");
        if (needToConfirm)
        {
            // Message to be displayed in Warning.
            return "Your data will be lost.";
        } 
        else
        {
            isDelete = false;
        }   
    }
  • 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-23T15:27:25+00:00Added an answer on May 23, 2026 at 3:27 pm

    I’m not certain; But I would of thought you cannot call another function, as a preventative measure by the browser so it doesn’t get trapped from leaving the page.

    I think only the browser knows if it’s returns true / false to determine if the page is unloaded (or not).

    Previously unbeforeunload was non-standard (from IE4, but supported by other browsers)
    Here are the Mozilla docs https://developer.mozilla.org/en/DOM/window.onbeforeunload
    And Microsoft docs: http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx

    However it looks as thought BeforeUnloadEvent is in the HTML5 proposal.
    This document gives in detail the steps that happen when a document is unloaded (interesting read):

    http://www.w3.org/TR/html5/history.html#beforeunloadevent


    As a workaround:
    You may be able to gather the information you need from the “unload event”, if the unload event is NOT called after the onbeforeunload, you could assume that the user chose to stay on your page.

    https://developer.mozilla.org/en/DOM/window.onunload

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

Sidebar

Related Questions

I am displaying a warning dialog box on window close event. If user clicks
http://www.neighborrow.com is currently displaying this error: Warning (512): SQL Error: 1054: Unknown column 'User.rating'
I am displaying a warning dialog box whenever user tries to navigate from current
Displaying Type here to ... until the user enters text into a TextBox is
I'm displaying a set of images as an overlay using Google Maps. Displaying these
I am displaying a texture that I want to manipulate without out affecting the
hi i got list of warning regarding file size when i try to upload
I have a form wizard. On step one I need to display a warning
I have used Multiple select Box in edit form, displaying previously selected items in
I'm displaying a message between Saturday at 6pm and Sunday 4am. The last time

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.