Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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
C#
window.onbeforeunload = askconfirm;
        window.doAskConfirm = true;

C#
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;
                }
            }
        }

C#
$(document).ready(function () {
            $("button").click(function () {
                window.doAskConfirm = true;
                if (this.id == "home")
                    document.location = '@Url.Action("Index", "Home")';
                else if (this.id == "grp")
                    document.location = '@Url.Action("ViewGroup", "GroupMaintenance")';
                else if (this.id == "client")
                    document.location = '@Url.Action("ClientMaintenanceHome", "ClientMaintenance")';
                else if (this.id == "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.

Thanks
Kishore
Posted

1 solution

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

in returnval you will get either true or false;

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

//write code for the cancel button clicked.

}

hope this helps....!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900