0

//window.onbeforeunload = function(e) { // return 'Are you sure you want to leave this page? You will lose any unsaved data.'; //};

    //document.attachEvent("onkeydown", my_onkeydown_handler);
    //function my_onkeydown_handler()
    //{
    //    switch (event.keyCode)
    //    {
    //        case 116 : // F5;
    //            event.returnValue = false;
    //            event.keyCode = 0;
    //            window.status = "We have disabled F5";
    //            break;
    //    }
    //}

    //window.onbeforeunload = function (e) {
    //    var message = "Are you sure you want to leave this page?  You will lose any unsaved data.",
    //    e = e || window.event;
    //    // For IE and Firefox
    //    if (e) {
    //        e.returnValue = message;
    //        //// ACTION
    //    }
    //    // For Safari//// ACTION
    //    return message;
    //};

    //(function () {
    //    window.unloader = function(e) {
    //        (e || window.event).returnValue = null;
    //        return null;
    //    };
    //    window.addEventListener("beforeunload", window.unloader);
    //})();

    //window.addEventListener("beforeunload", function (event) {
    //    event.preventDefault();
    //});

    var popit = true;
    window.onbeforeunload = function() { 
        if(popit == true) {
            popit = false;
            return "Are you sure you want to leave?"; 
        }
    }


    //var myEvent = window.attachEvent || window.addEventListener;
    //var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable

    //myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
    //    var confirmationMessage = 'Are you sure to leave the page?';  // a space
    //    (e || window.event).returnValue = confirmationMessage;
    //    return confirmationMessage;
    //});

    //window.addEventListener("beforeunload", function (e) {
    //    var confirmationMessage = "Hi";

    //    e.returnValue = confirmationMessage;     // Gecko, Trident, Chrome 34+
    //    return confirmationMessage;              // Gecko, WebKit, Chrome <34
    //});

    //$(window).bind("beforeunload",function(event) {
    //    return "You have some unsaved changes";
    //});

    //window.onbeforeunload = function (e) {
    //    e = e || window.event;

    //    var msg = "Sure you want to leave?";

    //    // For IE and Firefox prior to version 4
    //    if (e) {
    //        e.returnValue = msg;
    //    }

    //    // For Safari
    //    return msg;
    //};

    //window.onbeforeunload = function(evt) {
    //    return true;
    //}

    //window.onbeforeunload = function(evt) {
    //    var message = 'Are you sure?';
    //    if (typeof evt == 'undefined') {
    //        evt = window.event;
    //    }
    //    if (evt) {
    //        evt.returnValue = message;
    //    }

    //    return message;
    //}


    //window.addEventListener('beforeunload', function(event) {
    //    return 'Are you sure';
    //});

    //window.onunload  = confirmExit;
    //function confirmExit() {
    //    alert("Are you sure you want to leave");
    //}
    //$(function() {
    //    $("a").click(function() {
    //        window.onbeforeunload = null;
    //    });
    //    $("input").click(function() {
    //        window.onbeforeunload = null;
    //    });
    //});
    //window.onbeforeunload = function(){
    //    alert("Are you sure you want to leave");
    //}

I have tried all the above codes. They all work well with IE. But none of the codes works with Fire fox and Google Chrome. Can Someone help me out on this. I need to alert the end user with a alert box if he clicks the browsers close button.

  • Did you also uncomment the code during your tests? – huysentruitw Nov 07 '17 at 10:02
  • No . I did not comment the code during the tests –  Nov 07 '17 at 10:03
  • I think the _problem_ is just the fact that your Chrome or Firefox is configured to remember open-tabs, hence it probably does not execute the `onbeforeunload` handler on browser close. Try to change your browser settings and try again. – huysentruitw Nov 07 '17 at 10:04
  • Can you kindly tell me what is the setting which I should change in detail please. Thanks –  Nov 07 '17 at 10:06
  • In chrome: goto settings, scroll down to *On startup* and select _Open the new tab page_ instead of _Continue where you left off_, in Firefox you'll find a similar setting. – huysentruitw Nov 07 '17 at 10:08
  • But if this is the case, I downloaded firefox developer edition now. But it still does not work. –  Nov 07 '17 at 10:09
  • Possible duplicate of [Alert when browser window closed accidentally](https://stackoverflow.com/questions/1244535/alert-when-browser-window-closed-accidentally) – huysentruitw Nov 07 '17 at 10:12
  • Actually I tried both what you said. But its still the same –  Nov 07 '17 at 10:13
  • Alert when browser window closed accidentally . This does not work with all the three browsers –  Nov 07 '17 at 10:15

0 Answers0