tags:

views:

509

answers:

2

When a user have changed something in a form, then clicks on any link that will direct him to another page, I'd like to trigger a popup with a "Would you like to Save before leaving?" option.

How can I do this?

A: 

This is how it can be done but this is not always reliable:

<html>
<head>
<script type="text/javascript">
function leaving()
 {
    if(confirm("Would you like to save?"))
     {
     //Save info
     }
    else
     {
     //Don't save
     }
 }
</script>
</head>
<body onUnload="leaving()">
<!--Stuff-->
</body>
</html>
Unkwntech
+6  A: 

Example:

 <script type="text/javascript">
     var shouldConfirm = false;
     window.onbeforeunload = function() {
         if(shouldConfirm) {
             return "You have made unsaved changes. Would you still like to leave this page?";
         }
     }
 </script>

 <input id="FullName" type="text" />
 <script type="text/javascript">
     document.getElementById('FullName').onchange = function() {
         shouldConfirm = true;
     }
 </script>

There's a full article at 4GuysFromRolla.com.

EndangeredMassa