+ Start a Discussion
ksmoksmo 

popup message on navigating from a page without saving

I have a text box and few checkboxes on the page. If the user enters,changes existing text,clciks or unclicks the checkbox and clicks the cancel button or navigates away from the page I want a pop-up message saying yor changes have not been saved
Best Answer chosen by ksmo
sfdcFanBoysfdcFanBoy

Define a boolean variable called isChanged

Add an actionSupport onchange event to both the textField and the Checkbox.  On change call an apex method, the apex method will set the isChanged = true.

Now, in the apex method for Cancel button, redirect to the back page only if isChanged = false.  If its true, you can show a message.

All Answers

sfdcFanBoysfdcFanBoy

Define a boolean variable called isChanged

Add an actionSupport onchange event to both the textField and the Checkbox.  On change call an apex method, the apex method will set the isChanged = true.

Now, in the apex method for Cancel button, redirect to the back page only if isChanged = false.  If its true, you can show a message.

This was selected as the best answer
ksmoksmo
Hi sfdcFanBoy,

For example, below is the code for my text box, Is there any javascript solution for this
<div class="textpanel">
<input type="text" name="txt" size="35" />
</div>
sfdcFanBoysfdcFanBoy
Here's one example.  From input, we can call javascript function.  The javascript function call calls the actionFunction.  ActionFunction calls the apex method (to update the isChanged boolean
 
<table cellpadding="2" cellspacing="2">
  <tr>
    <td style="font-weight:bold;">Client Name
    <input type="text" id="clientName" onkeyup="doSearch()"/>
    </td>
  </tr>
</table>

<script type="text/javascript">
      function doSearch() {
        searchServer(
          document.getElementById("clientName").value);
      }
  </script>  

 <apex:actionFunction name="searchServer" action="{!UpdateIsChanged}" rerender="results,debug,errors">
      <apex:param name="clientName" value="" />          
  </apex:actionFunction>