Trying to detect browser close event

The Solution to Trying to detect browser close event is

Have you tried this code?

window.onbeforeunload = function (event) {
    var message = 'Important: Please click on \'Save\' button to leave this page.';
    if (typeof event == 'undefined') {
        event = window.event;
    if (event) {
        event.returnValue = message;
    return message;

$(function () {
    $("a").not('#lnkLogOut').click(function () {
        window.onbeforeunload = null;
    $(".btn").click(function () {
        window.onbeforeunload = null;

The second function is optional to avoid prompting while clicking on #lnkLogOut and .btn elements.

One more thing, The custom Prompt will not work in Firefox (even in latest version also). For more details about it, please go to this thread.

~ Answered on 2013-12-31 08:00:50

Most Viewed Questions: