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

  • Home
  • SEARCH
  • 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 8796153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:33:22+00:00 2026-06-13T23:33:22+00:00

Possible Duplicate: Is there a way to catch the back button event in javascript?

  • 0

Possible Duplicate:
Is there a way to catch the back button event in javascript?

In my application I want only a certain number of scripts (Text1.php, Text2.php and Text3.php) to contain a piece of code where that if the user clicks on the back browser button, it will display a confirmation box like the one below:

confirm("You have clicked on the browser's back button. If you confirm to go back via the browser back button, you will be logged out and lose your current assessment details." + "\n" + "\n" + "Are you sure you want to go back and lose current assessment details?" + "\n" );

The requirments are the following:

  1. How to show the confirm if the user clicks on the back browser button?
  2. If user confirms the confirmation then navigate to Text4.php (This is where it logs out user)
  3. If user cancels confirmation then just close confirmation box so user can continue what they are doing (guessing this is something to do with return false;
  4. Only want this to happen for scripts (Text1.php, Text2.php and Text3.php)
  5. Need it to work on all major browsers (IE, Chrome, Firefox, Safari, Opera)

Thank you

  • 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-13T23:33:24+00:00Added an answer on June 13, 2026 at 11:33 pm

    Note: You should ONLY do this if you NEED to prevent the person from leaving (that is, if they’ve inputted some important data and will lose it by navigating away). Also note that you cannot customize the confirmation popup in more recent browsers. (Older browsers allowed customizing the confirmation message body.) These are the restrictions of such a feature.

    You can use the beforeunload event to achieve this. In older browsers, you could return a custom string for the confirmation prompt:

    $(window).bind("beforeunload", function() {
        return "HI"; // This will be shown in the confirmation popup in older browsers.
    });​
    

    However, as mentioned by @formicini in the comments, more recent browsers (Chrome 87, for example) do not allow a custom string. Returning any non-undefined value will show the confirmation prompt, but will just display the browser’s generic message—even if it’s a string.

    $(window).bind("beforeunload", function() {
        return true; // Any non-undefined value will show generic confirmation popup.
    });​
    

    I would also suggest you unbind this upon someone leaving the page in the desired way (a cancel button?), or you may run into undesirable behavior.

    For example:

    $("a").click(function() {
        $(window).unbind("beforeunload");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is there a way to catch the back button event in javascript?
Possible Duplicate: is there a way to force event to fire only once on
Possible Duplicate: Is there a way to automatically update application on Android? As a
Possible Duplicate: Is there a way to automatically update application on Android? I have
Possible Duplicate: Is there some way to compile a .NET application to native code?
Possible Duplicate: Is there an easy way to convert a number to a word
Possible Duplicate: Catch multiple Exceptions at once? Is there any way in C# to
Possible Duplicate: Is there a (built-in) way in JavaScript to check if a string
Possible Duplicate: Android Button Tooltip Is there any way to construct a view where
Possible Duplicate: Is there a way to add a badge to an application icon

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.