Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the 'Are you sure you want to navigate away from this page' warning message in browser?

I have a web page that contains a textbox and a submit button. When the user edits the text in the textbox and clicks another link (not the submit button) how do I display the 'Are you sure you want to navigate away from this page' popup message?

I have researched this on the net and found a few javascript examples. Is this the only way you can do this? If not, what is the best way to do it?

like image 249
Theomax Avatar asked Dec 27 '22 05:12

Theomax


2 Answers

This is one of the multiple ways to achieve the same thing

function goodbye(e) {
    if(!e) e = window.event;
    //e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog

    //e.stopPropagation works in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
}
window.onbeforeunload=goodbye;

got it from here open js

like image 139
Andrei G Avatar answered Feb 09 '23 00:02

Andrei G


Only the unload() event will work on JS. You can't manage it on the server.

like image 25
napolux Avatar answered Feb 09 '23 00:02

napolux