Alert about data not saved before exiting browser

Alert about data not saved before exiting browser

by Paul Young -
Number of replies: 7

The users sometimes close the tab or browser before hitting the "Save and view" button. Is there way to trigger an alert message when this happens?

Thanks! 

Average of ratings: -
In reply to Paul Young

Re: Alert about data not saved before exiting browser

by Itamar Tzadok -
In reply to Itamar Tzadok

Re: Alert about data not saved before exiting browser

by Paul Young -

It worked flawlessly. Thank you!

In reply to Itamar Tzadok

Re: Alert about data not saved before exiting browser

by Paul Young -

Is there any way to disable the alert message when users click on "Save and view" to save the data?

In reply to Paul Young

Re: Alert about data not saved before exiting browser

by Itamar Tzadok -

One way is suggested in the linked example above where it shows how to use jquery to handle links. You just need to set the click event of the 'Save and view' button to run 

function{window.onbeforeunload =null;}

smile

In reply to Itamar Tzadok

Re: Alert about data not saved before exiting browser

by Paul Young -

If I understannd it correctly, this solution is the same as the other below it and both approaches would require hacking the /mod/data/edit.php file, with which I am definitely uncomfortable. Could you please give further instructions? Thanks!

 

In reply to Itamar Tzadok

Re: Alert about data not saved before exiting browser

by Paul Young -

For those who are interested, it turned out to be very easy.

1. Add the following javascript code in the Add template as suggested by the link provided by Itamar. 

<script type="text/javascript">
var hook = true;
window.Xonbeforeunload= function() {
if (hook) {
return "Did you save your stuff?"
}
}
function unhook() {
hook=false;
}
</script> 

 

2. Add the following code in line 316 and 321 of the edit.php file under moodle\mod\data\.

XonClick="unhook()"

 

In reply to Paul Young

Re: Alert about data not saved before exiting browser

by Itamar Tzadok -

An alternate way which does not involve hacking the php (and thereby avoids upgrade problems) is to use javascript for setting the onclick of the save button of the edit form to 'window.onbeforeunload = null'. That's the approach suggested in the afformentioned link. smile