Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the "Leave site? Changes that you made may not be saved" when trying to close a window with a populated form

I have a form that when filled in, I can exit the window without being given a warning (I'm using Chrome).

How can I ensure I get a warning before exiting? Is there a Django setting for this?

like image 757
Zorgan Avatar asked Aug 04 '18 00:08

Zorgan


2 Answers

You need to supply a function returning a string to onbeforeunload of the window or the document body in order to trigger the "Leave site" popup.

window.onbeforeunload = () => '';
<a href="http://example.com">Click to trigger</a>
like image 123
davidchoo12 Avatar answered Oct 31 '22 14:10

davidchoo12


The event is called beforeunload, so you can assign a function to window.onbeforeunload

like image 42
Scath Avatar answered Oct 31 '22 14:10

Scath