Confirm if user wants to leave page

Hi,

I was wondering what is the best method to create a custom prompt to ask the user if they are sure they want to leave the page.
Preferably I would like this confirmation to trigger if they hit the back button, attempt to close the tab, or attempt to navigate to a different page (unless the form on the page is submitted).

I’ve gotten success using window.onbeforeunload in a tag, however the ability for a custom message appears to be long gone for most browsers, and the generic message given by this is not ideal for this situation.

Apologies if this question has been asked before on these forums, I couldn’t seem to find anything through searching, If anyone can point me in the right direction it would be appreciated!

Community Page
Last updated: