How to use a Javascript Confirm box.

Category : Uncategorized

Example Scenario:
Lets say you have a form that has a BACK link. You want to make sure that if the user has made any changes on that form and they click the BACK that they REALLY want to leave the page.

So on your input boxes you can place an onChange event that sets a global variable to true (yes this form has been changed!)… i.e. onchange=”g_bformChanged=true;”

Then on your back button just fire off your function that checks to see if the form has changed… using your CONFIRM box to get a confirmation from your user.

  <script type="text/javascript">
      //Global
    var g_bformChanged = false;
 
   function checkForSaveNeed() {
        if (g_bformChanged) {
            var confirmLeave = confirm("No changes will be saved.  Are you sure you want to leave?");
            if (confirmLeave == true) {
                window.location = "ManageSchools.aspx"
            }            
        }
        else 
            window.location = "ManageSchools.aspx";
     }
</script>

Post a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word