Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confirm before onbeforeunload

I need to prompt the user before he leaves the page, on confirmation close the tab and if not do nothing. I want to send an ajax call on onbeforeunload.

My only idea was to write handlers for both onunload and onbeforeunload like this:

window.onbeforeunload = function(){
    return 'Are you sure you want to leave?';
};
window.onunload = function(){
    $.get(
        "http://www.mysite.com/php/myhandler.php",
        { data: 1 }
    );
};

but this did not seem to work in jsFiddle

like image 215
Nikita240 Avatar asked Nov 13 '22 00:11

Nikita240


1 Answers

It works fine in this fiddle--> Perfectly working fiddle!!!

 function warning(){
            if(true){
                return "You are leaving the page";
                 $.ajax({
                        url: "test.php",
                        type: "post",
                        data: values to send
                        })
            }
        }
        window.onbeforeunload = warning;
like image 123
HIRA THAKUR Avatar answered Nov 14 '22 22:11

HIRA THAKUR