javascript - 如何防止用户通过退出弹出窗口离开网站?JS
问题描述
我希望能够在每次用户尝试离开网站时重新出现我的网站副本。有这样的方法/事件处理程序吗?
不用说糟糕的 UI,我只想用事件处理程序探索更多
解决方案
没有办法阻止用户离开您的网站,更不用说在他们退出时生成一个新标签了。
这是一个类似的问题,讨论了为什么这是一个坏主意以及为什么大多数现代浏览器都删除了此功能:
下面的例子是纯理论的:
<!html>
<html>
<head>
<title>Persistent Page...</title>
<script type="text/javascript">
function closeHandler(e) {
e.preventDefault(); // Does not actually cancel
e.returnValue = ''; // Does prevent dialog (doesn't show anyways)
alert('Leaving website, opening a new tab...'); // Does not display
window.open(window.location.href, '_blank'); // Open a new tab?, No...
}
if (window.addEventListener) {
window.addEventListener('beforeunload', closeHandler, true);
} else if (window.attachEvent) {
window.attachEvent('onbeforeunload', closeHandler);
}
</script>
</head>
<body>
<h1>Persistent Page...</h1>
</body>
</html>
推荐阅读
- c - 我可以更有效地计算 2ⁿ 的数字总和吗?
- c# - 多选下拉项目在更新时自动选择属性
- django - 如何为 Angular 和 django 设置 Nginx 服务器
- cordova - 打开锁定屏幕 Ionic 4
- python - 如何计算递减点的数量?
- asp.net-core - 调用 ASP.NET Core 2.1 API 时出现 401。这是CORS问题吗?
- python - Debian gnu/linux 9.11 python 升级到 3.7
- webpack - es6 export const and import 在构建包的文件中包含整个模块,而不是我导入的内容
- google-app-engine - 更改 GCP DataProc 集群的调度程序属性
- python-3.x - 找到数字序列顶部或底部的点