Before user navigates the page code checks if he edited some of the form fields. If he did, I display a modal window with Yes
and No
buttons. If he
This is possible, but only if the user clicks a hyperlink within the page. If the user uses the browser to navigate away from the page, they will get the default browser dialog, which doesn't have an option to save.
Dirty Forms automatically attaches (and removes the handler) to the beforeunload
event, so your attempt to create another event handler will most certainly fail. You should never do this when using Dirty Forms.
You didn't mention which modal dialog framework you wanted to use, so I will just show an example using jQuery UI dialog. Integrating other dialog frameworks is similar. To do so, you might want to check out the source code of the existing pre-built dialogs.
Also, your use case is a bit short-sided. What if the user wants to navigate away and ignore the changes? I have added an example that includes an additional option to do just that.
Change one of the fields below, then click "Go to Google" to try to navigate away.
Go to Google
The example uses custom event binding to attach to the onRefireClick
event handler, which is fired when choice.proceed = true
, but not when it is false
.