I use this code on my website to display a warning message in a popup when a user trie to leave the page:
I would suggest capturing the submit event for the form and then either removing the confirmExit function from your onbeforeunload event binding or setting a flag to indicate that the page can be submitted. For example:
I'd recommend you consider using the addEventListener method for binding events in general, however given your example I've tried to keep my answer consistent.