防止单击 mailto 链接时调用 onbeforeunload
无论如何,有没有办法阻止在 Chrome 中单击 mailto 链接时调用 onbeforeunload 。 在 FF、Safari、IE 中运行良好。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
google.load("jquery", "1.3.2");
</script>
<script type="text/javascript">
$(document).ready(function(){
window.onbeforeunload = confirmExit;
});
function confirmExit() {
return "Are you sure?";
}
</script>
</head>
<body>
<a href="mailto:[email protected]?subject=test mail&body=Hello%20World">Mail Link</a>
</body>
</html>
Is there anyway to prevent onbeforeunload from being called when clicking on mailto link in chrome.
In FF, Safari, IE it is working fine.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
google.load("jquery", "1.3.2");
</script>
<script type="text/javascript">
$(document).ready(function(){
window.onbeforeunload = confirmExit;
});
function confirmExit() {
return "Are you sure?";
}
</script>
</head>
<body>
<a href="mailto:[email protected]?subject=test mail&body=Hello%20World">Mail Link</a>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有什么解决方法吗?
演示。
What about a workaround?
Demo.
这是一个看起来合理的建议解决方案
http://www.ilovebonnie.net/2009/09/23/how-to-use-onbeforeunload-with-form-submit-buttons/
更新(链接已失效)- 已复制来自 Google 缓存的内容
如何将 onbeforeunload 与表单提交按钮一起使用
2009 年 9 月 23 日 — Geekery
在进行一些编程时,我遇到了一个有趣的困境。虽然我知道让用户难以离开页面是邪恶的,但我并不是在这里争论 onbeforeunload 的优点(或缺乏优点)。
在特定表单上,我们强制浏览器不缓存信息,以避免潜在的 AJAX/JavaScript 问题(如果浏览器离开表单并返回),因为浏览器的行为并不相同(例如 IE 使复选框保持选中状态,但不这样做)记住由于 JavaScript 导致页面发生的更改)。因此,我们会在用户即将离开订单表单时发出警告,让他们知道需要再次填写订单。
该功能非常简单:
不幸的是,如果用户提交表单,这也会被触发,这显然不是我们想要的。在互联网上搜索后,我发现了一个简单的解决方案,我想我会分享:
由于单击提交按钮时onclick似乎在onsubmit之前注册,因此我们可以在提交按钮中添加一点变量声明,以便将submitFormOkay设置为true 在表单提交发生之前:
Here is a proposed solutions that looks reasonable
http://www.ilovebonnie.net/2009/09/23/how-to-use-onbeforeunload-with-form-submit-buttons/
UPDATE (link is dead) - Copied contents from Google Cache
How to Use onbeforeunload with Form Submit Buttons
September 23rd, 2009 — Geekery
While doing some programming I came across an interesting predicament. While I understand it’s evil to make it hard for a user to leave a page, I’m not here to argue the merits (or lack thereof) of onbeforeunload.
On a particular form, we are forcing the browser to not cache the information to avoid potential AJAX/JavaScript problems if they should leave the form and come back as browsers don’t all act the same (eg IE leaves checkboxes checked but doesn’t remember changes that have occurred to the page due to JavaScript). As such, we warn our users when they’re about to leave the order form to let them know they’ll need to fill it in again.
The function was simple enough:
Unfortunately, this would also be triggered if the user submitted the form which, is obviously not what we want. After searching around on the Internet I came across a simple solution that I thought I would share:
Since onclick appears to register before onsubmit when clicking a submit button, we can add a little variable declaration to our submit button so that submitFormOkay will be set to true before the form submission happens: