Warning before navigate away from a page

In some cases, if you are in between any work of editing or typing a content, and you wont let the visitor to navigate away from the page without particular condition, say the document is not saved yet, you can block the navigation (even the closing of the browser window) with Javascript.


window.onbeforeunload = function (evt) {
var message = ‘Are you sure you want to leave?’;
if (typeof evt == ‘undefined’) {
evt = window.event;
if (evt) {
evt.returnValue = message;
return message;

Permanent link to this article: https://blog.openshell.in/2011/02/warning-before-navigate-away-from-a-page/

Leave a Reply

Your email address will not be published.