防止onbeforeunload在任何情况下关闭页面

编程入门 行业动态 更新时间:2024-10-24 18:22:38
本文介绍了防止onbeforeunload在任何情况下关闭页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

无论如何,我都希望阻止浏览器关闭页面,在调用onbeforeunload时阻止浏览器执行任何操作. 这是我尝试过的代码.

I want to prevent browser to close page in any case or in other case, Prevent browser to do anything when onbeforeunload is called. Here is my code which i have tried.

(function() { var proxied = window.onbeforeunload; window.onbeforeunload = function(e) { e.preventDefault(); e.stopPropagation(); //i want to stop everything console.log('stay here'); // return 'message'; }; })();

  • 我想在离开页面之前执行一项操作(断开 聊天)
    • I want to perform a action before leaving the page (disconnect the chat)
    • 推荐答案

      您不能完全阻止用户离开页面(这将导致垃圾邮件/广告网站上的大量滥用,这些网站试图让您留在网站上页),但您可以显示诸如会引起用户确认提示的窗口之类的内容.看看防止网页使用JavaScript导航 a>可以引导您朝着正确的方向前进.

      You can't outright prevent a user from leaving the page (This would lead to much abuse on spam/advertisement sites who try to get you to stay on a page), but you can show things such as a window which causes a confirm prompt to the user. Have a look at Prevent a webpage from navigating away using JavaScript which can lead you to the right direction of what you're trying to accomplish.

更多推荐

防止onbeforeunload在任何情况下关闭页面

本文发布于:2023-11-29 00:58:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:情况下   页面   onbeforeunload

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!