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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:04:58+00:00 2026-05-27T00:04:58+00:00

I realize this is likely a duplicate, but I’ve been googling/SOing for a day

  • 0

I realize this is likely a duplicate, but I’ve been googling/SOing for a day now and I can’t find a satisfactory answer. If there is an answer already on SO, please send me there.

I have a client that insists on having an exit message popup confirming they want to exit the site, just like Gmail does. (I’ve already tried arguing against it. He is immovable, so no comments about how that is bad practice please.)

I’ve found this code:

<script>
    window.onbeforeunload = function() {
        return 'Are you sure you want to exit?';
    }
<script>

But it runs no matter what I do – reloading the page, clicking on the nav, etc.

I just want the message to show up when the user closes the tab/browser. I suspect it’s something simple I’m missing but I’m not a Javascript expert.

Any help would be greatly appreciated.

Thanks

EDIT

Here’s what is working pretty good. Thanks to all!

var isLeavingSite = true;

//This would be called on each link/button click that navigates
$('a, input[type="submit"]').click(function(){
    isLeavingSite = false;
});

window.onbeforeunload = function() {
    if(isLeavingSite)
    return 'Are you sure you want to exit?';
}
  • 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-27T00:04:59+00:00Added an answer on May 27, 2026 at 12:04 am

    Though it could be a fair amount of work (depending on how your site is written), you could do something like this (pseudo-code):

    var isLeavingSite = true;
    
    //This would be called on each link/button click that navigates
    function GlobalLinkHandler()
    {
        isLeavingSite = false;
    }
    
    window.onbeforeunload = function() {
        if(isLeavingSite)
        return 'Are you sure you want to exit?';
    }
    

    If you’re using jQuery, you can use the code below to flip the isLeavingSite flag:

    $('a, input[type="submit"]').click(function(){ isLeavingSite = false; });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this question is very likely to have been asked before, but I've
I realize that the answer to this question is likely quite obvious (if somewhat
I realize this is a basic question but I have searched online, been to
I realize this is a duplicate of about 20 different posts, but none of
I realize this question has most likely been beaten to death, and that it's
I realize there is likely a simple method to do this but how does
I realize this is likely to be an easy one, but my SQL is
I realize this is likely an inane question, but here goes: I'd like to
I realize this is syntactically bad but I figure it somewhat explains what I'm
I realize this is more of a hardware question, but this is also very

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.