Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery在Chrome和IE中的body元素上设置onbeforeunload_Javascript_Jquery_Google Chrome_Onbeforeunload - Fatal编程技术网

Javascript 使用jQuery在Chrome和IE中的body元素上设置onbeforeunload

Javascript 使用jQuery在Chrome和IE中的body元素上设置onbeforeunload,javascript,jquery,google-chrome,onbeforeunload,Javascript,Jquery,Google Chrome,Onbeforeunload,我有一个系统,在这个系统中,我想与用户确认,一旦设置了脏标志,他们是否想离开页面 我正在使用以下代码-在FireFox中,我可以通过FireBug查看页面源代码,并且标记正确地插入了onbeforeunload属性 在Chrome和FireFox中,这种情况不会发生,我可以在没有任何警告的情况下离开页面。更新body标记的jQuery行肯定正在执行,只是没有执行 if ($("body").attr('onbeforeunload') == null) { if (window.even

我有一个系统,在这个系统中,我想与用户确认,一旦设置了脏标志,他们是否想离开页面

我正在使用以下代码-在FireFox中,我可以通过FireBug查看页面源代码,并且标记正确地插入了onbeforeunload属性

在Chrome和FireFox中,这种情况不会发生,我可以在没有任何警告的情况下离开页面。更新body标记的jQuery行肯定正在执行,只是没有执行

if ($("body").attr('onbeforeunload') == null) {
    if (window.event) {
        // IE and Chrome use this
        $("body").attr('onbeforeunload', 'CatchLeavePage(event)');
    }
    else {
        // Firefox uses this
        $("body").attr('onbeforeunload', 'return false;CatchLeavePage(event)');
    }
}

您知道如何从这里开始吗?

您不能通过返回false来中止页面卸载。您必须返回将在消息框中显示给用户的字符串,用户决定是否离开或留在页面上(通过选择“确定”或“取消”按钮),因此您需要这样编写代码:

 window.onbeforeunload = function() {
    return "Are you sure you want to leave this page bla bla bla?"; // you can make this dynamic, ofcourse...
 };
试试这个

  <script type=\"text/javascript\">
        var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
        var leave_message = 'You sure you want to leave?'
        function goodbye(e) 
        {
            if(dont_confirm_leave!==1)
            {
                if(!e) e = window.event;
                //e.cancelBubble is supported by IE - this will kill the bubbling process.
                e.cancelBubble = true;
                e.returnValue = leave_message;
                //e.stopPropagation works in Firefox.
                if (e.stopPropagation) 
                {
                    e.stopPropagation();
                    e.preventDefault();
                }

                //return works for Chrome and Safari
                return leave_message;
            }
        }   
    window.onbeforeunload=goodbye;
    </script>

var不确认离开=0//如果希望用户能够在未经确认的情况下离开,请将“不确认离开”设置为1
var leave_message='确定要离开吗?'
功能(e)
{
如果(不确认离开!==1)
{
如果(!e)e=window.event;
//e、 IE支持cancelBubble-这将终止冒泡过程。
e、 取消气泡=真;
e、 returnValue=留言;
//e、 stopPropagation在Firefox中工作。
如果(如停止播放)
{
e、 停止传播();
e、 预防默认值();
}
//Chrome和Safari的回归作品
返回留言;
}
}   
window.onbeforeunload=再见;
检查此代码:

var validNavigation = false;

function wireUpEvents() {
var dont_confirm_leave = 0; 
var leave_message = "You sure you want to leave ?";

function goodbye(e) {
if (!validNavigation) {
if (dont_confirm_leave !== 1) {
if (!e) e = window.event;
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
}
}

window.onbeforeunload = goodbye;

document.onkeydown = function () {
switch (event.keyCode || e.which) {
case 116 : //F5 button
validNavigation = true;
case 114 : //F5 button
validNavigation = true;
case 82 : //R button
if (event.ctrlKey) {
validNavigation = true;
}
case 13 : //Press enter
validNavigation = true;
}

}
// Attach the event click for all links in the page
$("a").bind("click", function () {
validNavigation = true;
});

// Attach the event submit for all forms in the page
$("form").bind("submit", function () {
validNavigation = true;
});

// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function () {
validNavigation = true;
});
}

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function () {
wireUpEvents();
});

这并不漂亮,但它成功了

var warnclose = true;
var warn = function(e) {
    var warning = 'Your warning message.';
    if (warnclose) {
        // Disables multiple calls
        warnclose = false;

        // In case we still need warn to be called again 
        setTimeout(function(){
            warnclose = true;
        }, 500);

        return warning;
    }
};
window.onbeforeunload = warn;

我能够使用$(“body”).css(“margin”,“50px”);美元(“body”).attr(“test”、“hello”);看起来我现在无法设置onbeforeunload属性…天哪!你救了我一天。似乎krcko的假设是错误的:“您不能通过返回false来中止页面卸载”。或者他不够精确。看起来(仅在IE中)如果“onbeforeload”事件返回false,则不会显示任何内容。所以我正在处理的代码返回false,没有IE消息,但总是显示一条消息“false”,并请求离开。您的解决方案适用于所有浏览器<代码>(谢谢)2。
var warnclose = true;
var warn = function(e) {
    var warning = 'Your warning message.';
    if (warnclose) {
        // Disables multiple calls
        warnclose = false;

        // In case we still need warn to be called again 
        setTimeout(function(){
            warnclose = true;
        }, 500);

        return warning;
    }
};
window.onbeforeunload = warn;