Can I do ajax on 'Leave page' button? Below is my code:

window.onbeforeunload = function (event) {
    var message = '';
    if (typeof event == 'undefined') {
        event = window.event;
    }
    if (event) {
        event.returnValue = message;
    }
    return message;
}    

So you want a button that closes the users browser window, but opens a dialog first? The code works for a prompt if a user tries closing the window the normal way, not really sure why anyone would have a button on the page the closes a browser window though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.