单击链接时提示用户确认

发布于 2024-11-17 11:02:24 字数 426 浏览 8 评论 0原文

我有一个链接可以执行一些潜在危险的功能(删除某些内容)。我想提示用户是否真的想这样做。我想用 javascript 来做,应该很简单。

到目前为止我的解决方案:

<a href='/delete' onclick='return confirm("Are you sure?")'>delete</a>

.. 很糟糕,它不是由 Firefox 和 IE 中的中键单击触发的。

<a href='/delete' onmousedown='return confirm("Are you sure?")'>delete</a>

..不起作用。即使单击“确定”后返回 true,也不会导航链接。

实施这个的正确方法是什么?

I have a link that does some potentially dangerous function (delete something). I want to prompt user if he really wants to do it. I want to do it with javascript and it should be very simple.

My solutions so far:

<a href='/delete' onclick='return confirm("Are you sure?")'>delete</a>

.. is bad in a way that it is not triggered by middle click in Firefox and IE.

<a href='/delete' onmousedown='return confirm("Are you sure?")'>delete</a>

.. does not work. Even if true is returned on clicking Ok, link is not navigated.

What is the correct way to implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

稚气少女 2024-11-24 11:02:25
<a href='#' onclick='confirmUser()'>delete</a>

javascript

 function confirmUser(){
    var ask=confirm("Are you sure");
    if(ask){
      window.location="/delete";
     }
}
<a href='#' onclick='confirmUser()'>delete</a>

javascript

 function confirmUser(){
    var ask=confirm("Are you sure");
    if(ask){
      window.location="/delete";
     }
}
一身仙ぐ女味 2024-11-24 11:02:25

我刚刚为网上银行客户解决了这个问题。每当用户导航到第三方网站时,FDIC 都要求确认“减速”。我们希望做到不引人注意,让人们无法绕过。

我已经用 IE、Firefox、Chrome 和 Android 对此进行了测试——单击、右键单击、中键单击、Shift 单击、Shift F10、Enter、长按,等等(Firefox 是最难的)。要么你遇到减速带,要么你得到一个空白选项卡,你无法绕过它。

document.ready = function() {

    var handleSpeedBump = function(e) {
        e.preventDefault();

        var href = this.getAttribute("data-href");
        var sure = confirm("Are you sure you want to navigate to " + href + "?  This is a third party site and not owned by this institution.");
        if (!sure) return;
        document.location = href;
    }

    $("a.speedbump")
        .click(handleSpeedBump)
        .bind("contextmenu", handleSpeedBump)
        .dblclick(handleSpeedBump)
        .each(function() {
            var href = this.href;
            this.setAttribute("data-href", href);
            this.href = "javascript:void('Navigate to " + href.replace("'", "") + "')";
        })
}

要实现此功能,只需按照正常情况编写链接并将“speedbump”添加到其类中即可。

<A href="www.thirdpartysite.com" class="speedbump">Here is the link!</A>

I just solved this problem for an online banking client. FDIC requires a confirmation "speedbump" whenever the user is navigating to a third party site. We wanted to do it unobtrusively, and make it impossible to get around.

I've tested this with IE, Firefox, Chrome, and Android-- click, right click, middle click, shift click, shift F10, enter, long tap, everything (Firefox is the hard one). Either you get the speedbump or you get a blank tab, you can't get around it.

document.ready = function() {

    var handleSpeedBump = function(e) {
        e.preventDefault();

        var href = this.getAttribute("data-href");
        var sure = confirm("Are you sure you want to navigate to " + href + "?  This is a third party site and not owned by this institution.");
        if (!sure) return;
        document.location = href;
    }

    $("a.speedbump")
        .click(handleSpeedBump)
        .bind("contextmenu", handleSpeedBump)
        .dblclick(handleSpeedBump)
        .each(function() {
            var href = this.href;
            this.setAttribute("data-href", href);
            this.href = "javascript:void('Navigate to " + href.replace("'", "") + "')";
        })
}

To make this work, just write your link per normal and add "speedbump" to its classes.

<A href="www.thirdpartysite.com" class="speedbump">Here is the link!</A>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文