8

潜在的に危険な機能 (何かを削除する) を行うリンクがあります。ユーザーが本当にやりたいかどうかを確認したい。私は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>

.. 動作しません。OK をクリックして true が返されても、リンクはナビゲートされません。

これを実装する正しい方法は何ですか?

4

2 に答える 2

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

JavaScript

 function confirmUser(){
    var ask=confirm("Are you sure");
    if(ask){
      window.location="/delete";
     }
}
于 2011-06-29T09:23:50.233 に答える
5

オンラインバンキングクライアントのこの問題を解決しました。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("'", "") + "')";
        })
}

これを機能させるには、通常どおりにリンクを作成し、そのクラスに「スピードバンプ」を追加するだけです。

<A href="www.thirdpartysite.com" class="speedbump">Here is the link!</A>
于 2013-12-19T01:36:18.433 に答える