防止单击 mailto 链接时调用 onbeforeunload

发布于 2024-08-11 08:56:03 字数 838 浏览 10 评论 0原文

无论如何,有没有办法阻止在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凌乱心跳 2024-08-18 08:56:03

有什么解决方法吗?

$(document).ready(function(){
    mailtoClicked = false;
    window.onbeforeunload = confirmExit;
    //Test if browser is Chrome
    if (/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())) {
        $('a[href^=mailto]').click(function() {mailtoClicked = true;});
    }
});

function confirmExit() {
    if (!mailtoClicked) {
        return "Are you sure?";
    } else {
        mailtoClicked = false;
    }
}

演示

What about a workaround?

$(document).ready(function(){
    mailtoClicked = false;
    window.onbeforeunload = confirmExit;
    //Test if browser is Chrome
    if (/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())) {
        $('a[href^=mailto]').click(function() {mailtoClicked = true;});
    }
});

function confirmExit() {
    if (!mailtoClicked) {
        return "Are you sure?";
    } else {
        mailtoClicked = false;
    }
}

Demo.

不寐倦长更 2024-08-18 08:56:03

这是一个看起来合理的建议解决方案

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 导致页面发生的更改)。因此,我们会在用户即将离开订单表单时发出警告,让他们知道需要再次填写订单。

该功能非常简单:

window.onbeforeunload = function () {
    return "You are about to leave this order form. You will lose any information...";
}

不幸的是,如果用户提交表单,这也会被触发,这显然不是我们想要的。在互联网上搜索后,我发现了一个简单的解决方案,我想我会分享:

// Create a variable that can be set upon form submission
var submitFormOkay = false;
window.onbeforeunload = function () {
    if (!submitFormOkay) {
        return "You are about to leave this order form. You will lose any information...";
    }
}

由于单击提交按钮时onclick似乎在onsubmit之前注册,因此我们可以在提交按钮中添加一点变量声明,以便将submitFormOkay设置为true 在表单提交发生之前:

<input type="submit" id="submit_button" onclick="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:

window.onbeforeunload = function () {
    return "You are about to leave this order form. You will lose any information...";
}

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:

// Create a variable that can be set upon form submission
var submitFormOkay = false;
window.onbeforeunload = function () {
    if (!submitFormOkay) {
        return "You are about to leave this order form. You will lose any information...";
    }
}

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:

<input type="submit" id="submit_button" onclick="submitFormOkay = true;">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文