Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: jQuery JQueryUI
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
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 (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 22-May-13 23:59pm

1 solution

Rate this: bad
good
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;
 
if(returnval==true)
{
//write code for the ok button clicked.
}
else if(returnval==false)
{
 
//write code for the cancel button clicked.
 
}
 
hope this helps....!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 190
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 Afzaal Ahmad Zeeshan 120
4 DamithSL 115
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 23 May 2013
Copyright © CodeProject, 1999-2014
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