单击链接时提示用户确认

Prompt user for confirmation when clicking a link

本文关键字:用户 确认 提示 链接 单击      更新时间:2023-09-26

我有一个链接,它执行一些潜在的危险功能(删除某些内容(。如果用户真的想做,我想提示他。我想用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,也不会导航链接。

实现这一点的正确方法是什么?

<a href='#' onclick='confirmUser()'>delete</a>

javascript

 function confirmUser(){
    var ask=confirm("Are you sure");
    if(ask){
      window.location="/delete";
     }
}

我刚刚为一个网上银行客户解决了这个问题。每当用户导航到第三方网站时,FDIC都需要一个确认"减速带"。我们想不引人注目地做这件事,让它无法四处走动。

我已经在IE、Firefox、Chrome和Android上测试过了——点击、右键点击、中键点击、移位点击、移位F10、回车、长按等等(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("'", "") + "')";
        })
}

要做到这一点,只需按照法线编写链接,并将"减速带"添加到其类中。

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