首页 > 解决方案 > 右键单击禁用不适用于滚动按钮单击,它显示警报但在新选项卡中打开

问题描述

我正在为我的网站禁用右键单击,但是当我使用鼠标并单击滚动按钮时,它会显示警报,然后单击确定后它会在新选项卡中打开。我怎样才能禁用它?下面是我的 Javascript 代码。请帮帮我。

// Right Click Disable Functions starts here

function clickIE()
{
    if (document.all) 
    {
        alert("Right click is disabled for Security Reasons.");
        return false;
    }
 }

function clickNS(e) 
{  
    if(document.layers||(document.getElementById&&!document.all)) 
    {
        if (e.which==2||e.which==3)
        {
            alert("Right click is disabled for Security Reasons.");
            return false;
        }
    }    
}

if (document.layers)
{
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown=clickNS;
}
else
{
    document.onmouseup=clickNS;document.oncontextmenu=clickIE;
}
    document.oncontextmenu=new Function("return false")

// Right Click Disable Functions Ends here

标签: javascriptdom-eventsmouseeventcontextmenu

解决方案


mouseup事件在mousedown事件之后触发。您需要的是防止mousedown事件(用于右键和中键)。所以你只需要使用document.onmousedown insted of document.onmouseup


推荐阅读