如何在单击浏览器关闭按钮时提醒用户

时间:2017-11-07 10:01:20

标签: javascript asp.net

// window.onbeforeunload = function(e){         //返回'您确定要离开此页面吗?您将丢失所有未保存的数据。&#39 ;;         //};

    //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");
    //}

我已经尝试了以上所有代码。它们都适用于IE。但是这些代码都不适用于Fire fox和Google Chrome。有人可以帮我解决这个问题。如果他点击浏览器关闭按钮,我需要通过警告框提醒最终用户。

0 个答案:

没有答案