javascript 确认浏览器后退按钮,否则留在页面上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3681093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:55:36  来源:igfitidea点击:

Confirm browser back button else stay on page

javascriptjquery

提问by Stephen Way

In javascript or jquery I need to add an alert when the user clicks on the browser back button that has an ok/cancel button model, but instead of "Ok" it should say Leave and instead of Cancel it should say Stay. Thanks

在 javascript 或 jquery 中,当用户单击具有确定/取消按钮模型的浏览器后退按钮时,我需要添加一个警报,但不是“确定”,而是“离开”,而不是“取消”,而是“保持”。谢谢

回答by Nick Craver

You can't control the confirmation dialog button text, it's a hard coded feature of confirm()and is whatever the browser has...not much you can do about it.

您无法控制确认对话框按钮文本,它是一个硬编码功能,confirm()并且是浏览器所具有的任何功能……您对此无能为力。

For the actual display you can use window.onbeforeunload, but it won't be specific to the back button, any action leaving the page will trigger this, for example:

对于实际显示,您可以使用window.onbeforeunload,但它不会特定于后退按钮,离开页面的任何操作都会触发此操作,例如:

window.onbeforeunload = function() {
  return "Are you sure you wish to leave this delightful page?";
}