window.conf在FireFox中不起作用
window.confirm not working in FireFox
背景:目标是防止用户使用退格键返回页面。除了一些输入字段之外,我已经创建了禁用键的代码。但如果他们真的想回去,我想在确认对话框中询问他们是否真的想回来。
问题:以下代码适用于IE和Chrome,但不适用于FF。确认会弹出,但仍会"返回"一页。这在IE/Chrome中不会发生,因为确认对话框会等待用户输入。
代码:
<script type="text/javascript">
$(document).unbind('keydown').bind('keydown', function (event) {
var doPrevent = false;
if (event.keyCode === 8) {
var d = event.srcElement || event.target;
if ((d.tagName.toUpperCase() === 'INPUT' &&
(
d.type.toUpperCase() === 'TEXT' ||
d.type.toUpperCase() === 'PASSWORD' ||
d.type.toUpperCase() === 'FILE' ||
d.type.toUpperCase() === 'SEARCH' ||
d.type.toUpperCase() === 'EMAIL' ||
d.type.toUpperCase() === 'NUMBER' ||
d.type.toUpperCase() === 'DATE' )
) ||
d.tagName.toUpperCase() === 'TEXTAREA') {
doPrevent = d.readOnly || d.disabled;
}
else {
var r = window.confirm("Leaving the page can cause data to be lost. Are you sure?");
if (!r) {
doPrevent = true;
}
}
}
if (doPrevent) {
event.preventDefault();
//event.stopPropagation();
}
});
</script>
这修复了它,并在每个浏览器中工作(Safari也是):
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "Leaving the page can cause data to be lost. Are you sure?";
e.returnValue = confirmationMessage; // Gecko and Trident
return confirmationMessage; // Gecko and WebKit
});
相关文章:
- 为什么javascript:void(0)在Firefox中不起作用
- jpm的默认Firefox路径没有'不起作用
- 设置 CSS 背景在 Firefox 中不起作用
- document.getElementById 在 Firefox 中不起作用
- firefox中的CSS动画不起作用
- event.stopPropagation()在firefox javascript中不起作用
- 复选框.onClick在firefox中不起作用
- window.close();在Chrome和Firefox中不起作用
- JQuery在Chrome中不起作用,但Firefox很好
- 选择“行在 Chrome 和 Firefox 中不起作用(生成的代码)”
- Javascript 按键事件在 Firefox 中不起作用
- window.dispatchEvent 在 Firefox、Safari 或 IE 中不起作用
- 更改 Angular 2 中选择的事件在 Firefox 和 Edge 中不起作用
- jQuery SlideToggle() 在 FireFox 中不起作用,在 Chrome 中工作
- 动态关键帧插入在 IE 或 Firefox 上不起作用
- 使用 nameProp 的 JQuery 图像滚动更新在 Firefox 和 Chrome 中不起作用
- Javascript键码在Firefox中不起作用,但在chrome中运行良好
- HTML5<视频>元素在Firefox、Opera和Safari中不起作用
- 输入类型的图像在Internet Explorer中不起作用,但在Chrome和Firefox中起作用
- Ajax 提交表单不起作用 Firefox