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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:51:27+00:00 2026-06-08T02:51:27+00:00

I have the following javascript to prompt the user if he attempts to navigate

  • 0

I have the following javascript to prompt the user if he attempts to navigate away from the page. A dialog box will appear with 2 buttons ‘Leave this page’ and ‘Stay on this page’. I would like to cause a postback which will fire a C# Event.

I want this postback to occur when the ‘Leave this page’ button is pressed.

How can i do this? Pardon me i am quite new to Javascript.

<script type="text/javascript" language="JavaScript">
var needToConfirm = true;

window.onbeforeunload = confirmExit;
function confirmExit() {
    if (needToConfirm)
        return "You have attempted to leave this page. This will CANCEL your current order selection.";
}

  • 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-08T02:51:28+00:00Added an answer on June 8, 2026 at 2:51 am

    The main challenge here is to execute the JavaScript function before the user actually leaves the page, the problem is that handling the onbeforeunload event, you cannot react to the user choice because in order for this event to work in multi-browser scenarios, you must return a string which will be used as the message, and each browser will create a custom dialog that will be shown to the user. I have not found a better approach, to do what you require. Basically you cannot override the default behavior in the browsers, and you cannot return a bool to indicate whether the page should be unloaded or not.

    For more info:

    • How can I override the OnBeforeUnload dialog and replace it with my own?

    • jQuery UI Dialog OnBeforeUnload

    From MSDN

    When a string is assigned to the returnValue property of window.event, a dialog box appears that gives users the option to stay on the current page and retain the string that was assigned to it. The default statement that appears in the dialog box, “Are you sure you want to navigate away from this page? … Press OK to continue, or Cancel to stay on the current page.”, cannot be removed or altered.

    So in this example, I’m proposing a workaround, I’m not sure if this is the best approach, but I have seen behavior like this in several online banks and from Microsoft itself when logging out in their sites.

    Example:

    ASPX

    <script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        function showConfirmOnLeave() {
            return $("#showConfirm").prop("checked");
        }
        function onLeavePage() {
            if (showConfirmOnLeave()) {
                return "Don't goo";
            }
            else {
                return undefined;
            }
        }
        $(function () {
            if ($("body").attr('onbeforeunload') == null) {
                $("body").attr('onbeforeunload', 'return onLeavePage(event)');
            }
            $(window).unload(function (event) {
                if (showConfirmOnLeave()) {
                    window.open(
                        '<%: this.ResolveClientUrl("~/CallOnLeave.aspx") %>',
                        "_blank",
                        "height=100, location=no, menubar=no, resizable=no, scrollbars=no, status=no, titlebar=no, toolbar=no, width=100, left=1, top=1",
                        false
                    );
                }
            });
        });
    </script>
    
    <input type="checkbox" id="showConfirm" />  Show confirm on exit?
    

    CallOnLeave.ASPX code behind

        protected void Page_Load(object sender, EventArgs e)
        {
            // add here your custom code, ideally fire a new async thread to execute your commands on the background
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following javascript in my page which does not seem to be
I have the following Javascript libraries loaded for my page. <script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js></script> <script
I have the following javascript: $.post(/Authenticated/DeletePage/ + PageId); showStatus(Page deleted..., 10000); I would like
I have applied the following code in javascript to pop-up a input box for
I have the following javascript, which I want to use to enable the user
I have the following JavaScript in my page to get the location of the
I have the following javascript being called to request a focus when page is
I have the following javascript functions, which when in a standalone file, will be
I have the following javascript on my page: var isDirty = false; function OnTextBoxValueChanged(sender,
I have the following javascript code that runs when a page is closed. $(window).unload(function

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.