Click here to Skip to main content
13,091,145 members (51,665 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I am working on the jquery. If user wants to navigate to any othere page then the confirm box will be displayed with the message "Are you sure you want to navigate current page". For this i wrote below code
window.onbeforeunload = askconfirm;
        window.doAskConfirm = true;

function askconfirm() {
            if (doAskConfirm) {
                if (confirm("Do you want to leave this page?") == true) {
                    //they pressed OK
                } else {
                    // they pressed Cancel
                    window.onbeforeunload = null;
                    window.doAskConfirm = false;

$(document).ready(function () {
            $("button").click(function () {
                window.doAskConfirm = true;
                if ( == "home")
                    document.location = '@Url.Action("Index", "Home")';
                else if ( == "grp")
                    document.location = '@Url.Action("ViewGroup", "GroupMaintenance")';
                else if ( == "client")
                    document.location = '@Url.Action("ClientMaintenanceHome", "ClientMaintenance")';
                else if ( == "logout")
                    document.location = '@Url.Action("LogOut", "Login")';
                else {
                    window.doAskConfirm = false;
                    return false;

My Question is how to handle cancel button because when i click on the cancel button script error is giving.

Can any body tell me how to handle cancel the confirm box.

Posted 22-May-13 22:59pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

write like this.
var returnval= confirm("Do you want to leave this page?");

in returnval you will get either true or false;

//write code for the ok button clicked.
else if(returnval==false)

//write code for the cancel button clicked.


hope this helps....!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 23 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100