页面关闭前的提醒:如何更改Chrome的默认消息? [英] Alert Before Page Close: How to change the Chrome's default message?

查看:1461
本文介绍了页面关闭前的提醒:如何更改Chrome的默认消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码片段在页面关闭前触发警报,但Chrome似乎忽略了该消息并显示其默认消息您想离开本网站吗?您所做的更改可能无法保存。

  window.onbeforeunload = function(e){$ b $我如何使chrome显示我的消息而不是默认的消息? b e.returnValue =搜索正在进行中,你真的想停止搜索并关闭标签吗?; 
return搜索正在进行中,你真的想停止搜索并关闭标签吗?;
}


解决方案

已经改变了onbeforeunload的行为。我发现了一个适用于主要浏览器的解决方法(在Chrome,Firefox和IE中测试过,由于某种原因,jsFiddle在Firefox中不起作用,但是我的网站却是这样)。使用jQuery UI,您可以在离开页面时出现一个对话窗口,该页面提供了更多关于为什么离开当前页面会出现问题的信息。

  window.onbeforeunload = function(e){
$('< div title =Warning!>搜索正在进行中,您是否确实要停止搜索并关闭标签({
modal:true,
close:function(){$(this).dialog)如果没有,请在浏览器提示中选择停留在页面上。< / div> ('destroy')。remove();}
});
返回随机消息触发浏览器的本地警报。;
}

jsFiddle here

I am using the following code snippet to trigger an alert before page closes but Chrome seems to ignore the message and displays its default message "Do you want to leave this site? Changes you made might not be saved". How can I make chrome show my message instead of the default one?

window.onbeforeunload = function(e) {
    e.returnValue = "A search is in progress, do you really want to stop the search and close the tab?";
    return "A search is in progress, do you really want to stop the search and close the tab?";
}

解决方案

I only recently realized that Chrome had changed the behavior of onbeforeunload. I found a workaround that works for the main browsers (tested in Chrome, Firefox and IE, the jsFiddle for some reason doesn't work in Firefox, but my website does). Using jQuery UI, you can make a dialog window come up when leaving the page that gives more info about why leaving the current page would be a problem.

window.onbeforeunload = function (e) {
    $('<div title="Warning!">A search is in progress, do you really want to stop the search and close the tab? If not, choose "Stay on page" on the browser alert.</div>').dialog({
        modal:true,
        close: function(){$(this).dialog('destroy').remove();}
    });
    return "Random message to trigger the browser's native alert.";
}

jsFiddle here

这篇关于页面关闭前的提醒:如何更改Chrome的默认消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆