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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:28:46+00:00 2026-05-12T11:28:46+00:00

I have a requirement that when the user clicks on [X] button of the

  • 0

I have a requirement that when the user clicks on [X] button of the browser, I should give them a message.

I accomplished this task using the onUnload method of JavaScript.

Now, if even I refresh the page, then also the same message appears!

Why this is so and how to overcome?

The code is here

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<script language = “javascript”>

function Message()

{
alert(“Are you sre you want to leave?”);
}

</script>
</HEAD>

<BODY onUnload=”Message();”>
</BODY></HTML>

Please help

  • 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-12T11:28:46+00:00Added an answer on May 12, 2026 at 11:28 am

    That’s correct behavior: refreshing the page involves unloading it and then loading it again.

    The best way to approach such a confirm dialog is with code like this:

    <script language="javascript" type="text/javascript">
    window.onbeforeunload = function() {
        return "You have unsaved changes.";
    }
    </script>
    

    This is the mechanism you see, for example, on GMail if you try to close a page when your message hasn’t been saved or sent yet. It will still fire when attempting to refresh the page, so it’s important to ensure you only return an error string if there really is unsaved work.

    Please keep in mind that there’s no guarantee the onUnload or onBeforeUnload events will fire, since there’s no guarantee your visitor will even have enabled JavaScript, so you shouldn’t use this for anything more than a friendly reminder about unsaved work.

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

Sidebar

Related Questions

I have requirement that if user click on a button then the li should
I have a business requirement that when the user clicks a series of checkboxes
In my ASP.Net application I have a requirement that when a user clicks on
I have a requirement that a user is allowed only to enter string value
In our project we have requirement that, after receiving sms message from third party
I have an Email Id text box. My requirement is that an user enters
My requirement is when the user clicks the submit button, a new value calculated
I have a requirement when a user clicks a specific arrangement of radio buttons
I have a bunch of controls: When a user clicks the Generate button, a
I have received requirements that ask to normalize text box content when the user

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.