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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:10:43+00:00 2026-05-15T01:10:43+00:00

When I close my page on some browser I want a message box to

  • 0

When I close my page on some browser I want a message box to appear and to ask me if I really want to close the page or not, with two buttons and if I click No then this tab won’t be closed.

How can I do that?

  • 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-15T01:10:44+00:00Added an answer on May 15, 2026 at 1:10 am

    You need to listen on the beforeunload event.

    Here’s a kickoff example:

    window.onbeforeunload = function() {
        return "Hey, you're leaving the site. Bye!";
    };
    

    This message will show up in kind of a confirmation dialogue. This message will show up right before the client unloads the page. That can be a browser close, but that can also be a simple navigational action like clicking a link or submitting a form in the page!

    You would most probably also like to turn it off (just set to null) whenever an internal link is clicked or an internal form is submitted. You namely don’t want to annoy endusers with unintuitive behaviour. You can do that by listening on the click event of the desired links and the submit event of the desired forms. jQuery may be of great help here since it does that in crossbrowsercompatible way so that you don’t need to write >20 lines of JS code for this:

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        window.onbeforeunload = function() {
            return "You're leaving the site.";
        };
        $(document).ready(function() {
            $('a[rel!=ext]').click(function() { window.onbeforeunload = null; });
            $('form').submit(function() { window.onbeforeunload = null; });
        });
    </script>
    

    You only need to give all external links the defacto standard attribute rel="ext" to denote that those are external links.

    <a href="http://google.com" rel="ext">Google</a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to close an ASPX page from the code-behind? I
I want to close a System.Windows.Forms.Form if the user clicks anywhere outside it. I've
Connection.close() may throw SqlException , but I have always assumed that it is safe
Is it possible to close an InfoPath form programmatically? I know that it can
When trying to call Close or Dispose on an SqlDataReader i get a timeout
Vista displays the dialog Close programs to prevent information loss when the system is
This question comes close to what I need, but my scenario is slightly different.
Does Java Connection.close rollback into a finally block?. I know .Net SqlConnection.close does it.
Should log classes open/close a log file stream on each write to the log
I hope this is close enough to programming to pass the bar :) Is

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.