onbeforeunload 确认屏幕自定义
是否可以在浏览器中为 onbeforeunload 事件创建自定义确认框?我尝试过,但随后我收到了 2 个确认框(其中一个来自我,无非是返回确认...然后是来自浏览器的标准确认框)。
目前我的代码如下所示:
var inputChanged = false;
$(window).load(function() {
window.onbeforeunload = navigateAway;
$(':input').bind('change', function() { inputChanged = true; });
});
function navigateAway(){
if(inputChanged){
return 'Are you sure you want to navigate away?';
}
}
我正在使用 jQuery 来实现此目的。
Is it possible to create a custom confirmation box for the onbeforeunload event in a browser? I tried but then I get 2 confirmation boxes (one from me which is nothing more than return confirm... and then the standard one from the browser).
At the moment my code looks like:
var inputChanged = false;
$(window).load(function() {
window.onbeforeunload = navigateAway;
$(':input').bind('change', function() { inputChanged = true; });
});
function navigateAway(){
if(inputChanged){
return 'Are you sure you want to navigate away?';
}
}
I'm using jQuery for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意:大多数浏览器将此消息放在其他一些文本之后。您无法完全控制确认对话框的内容。
Please note: Most browsers put this message after some other text. You do not have complete control of the content of the confirmation dialog.
不,您无法避免浏览器中的标准版本。您所能做的就是向其中注入一些自定义文本;如果您使用以下事件处理程序(注册原型库方式):(
并且
showMyBeforeUnloadConfirmation
为 true),您将获得浏览器的标准确认,其中包含以下文本:No, you can't avoid the standard one from the browser. All you can do is inject some custom text into it; if you use the following event handler (registered the prototype lib way):
(and
showMyBeforeUnloadConfirmation
is true) you'll get the browser's standard confirmation with the following text:我遇到了同样的问题,我能够获得带有消息的自己的对话框,但我面临的问题是:
以下是我找到的解决方案代码,我将其写在我的母版页上。
I faced the same problem, I was able to get its own dialog box with my message, but the problems I faced were:
Following is the solutions code I found, which I wrote on my Master page.