I have a web form on my web page. Before the user leaves the page, I want a javascript popup message asking the user if they have completed the form with a "Yes" or "No" button on it. If the user clicks "Yes", then they are brought to the page they intended to go to. However, if the user clicks "No", then the user remains on this page.
I am not very familiar with Javascript so any guidance would be much appreciated. I am suspecting that I would use something like below:
<ELEMENT onbeforeunload = "handler" ... >
Thanks in advance.
Split P.
2 1 thing (thanks @horray I'm helping).
You're better off detecting whether the form has been filled out programmatically, then if the user clicks to continue on without providing a critical piece of data, ask them for it. If they decide not to provide it, let them go.
For example:
var requiredField = document.getElementById("required");
if (requiredField.value == "")
{
if (!confirm("Are you sure you want to go without saving your work?")){
requiredField.focus();
}
}
You could put that in click handlers for links, submit buttons or in the beforeunload event as you mentioned.