[英]Custom message before leaving a page with current browsers
在页面上,我有一个复杂的表单(许多字段,选项卡...),我想显示一条警告消息,警告用户如果他尝试离开页面,无论他是否发布了表单(因为在编辑中)表格中,您已设置了所有必填字段,但可以修改值,也可以不修改值)。
我已经看到更改警报消息的可能性已被删除,因为Chrome 51仍然无法在其他浏览器上使用,但是我想在页面更改之前显示一条消息,但是我想在此之前显示它实际的默认Chrome / FF,IE ...弹出警报 。
在这种情况下,我处于或多或少的状态,但Chrome 51之后的答案不再相关。
所以,从这个问题
我做了
(function ($) {
'use strict';
$(window).bind('beforeunload', function(e) {
e.preventDefault();
$('<div title="Attention !">Vous souhaitez quitter cette page. Avez-vous enregistré les données du formulaire ? Si non, choisissez "rester sur la page" lors de l\'affichage de l\'alerte.</div>').dialog({
modal:true,
close: function(){$(this).dialog('destroy').remove();},
position: { my: "center top", at: "center", of: window }
});
return "Random message to trigger the browser's native alert.";
});
}(jQuery));
看到小提琴
但这会在浏览器警报后显示jQueryUI警报消息,因此实际上没有用。
自2017年浏览器使用以来,如何在实际浏览器警报之前显示此jquery警报? 谢谢
您可以尝试另一种实现:
<html>
<head>
<script type="text/javascript">
var hook = true;
window.onbeforeunload = function() {
if (hook) {
return "Did you save your stuff?"
}
}
function unhook() {
hook=false;
}
</script>
</head>
<body>
<!-- this will ask for confirmation: -->
<a href="http://google.com">external link</a>
<!-- this will go without asking: -->
<a href="anotherPage.html" onClick="unhook()">internal link, un-hooked</a>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.