I added the following script to prevent leaving the page during processing steps:
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
JQObj.ajax({
type: "POST",
url: "<?php echo $this->url(array('controller'=>'question','action'=>'cleaSess'), 'default', true); ?>",
success: function(data){}
});
return "You are trying to leave this page. If you have made changes in the fields without saving, they will be lost. Are you sure you want to exit?";
}
</script>
However, I keep getting the default alert message instead of the custom one I set.
I also want to trigger the ajax call when the end user clicks on the "Leave page" button, but currently, the ajax call is triggered before clicking the leave button in the script above.
Does anyone have an idea or a solution to only call the ajax when people actually leave the page?