我想在firefox和IE中禁用右键单击选项

时间:2017-07-19 08:22:32

标签: javascript jquery internet-explorer firefox mozilla

根据我的要求,我有一个PDF文件显示给用户。但是用户不应该保存它。所以,我需要禁用浏览器的右键单击选项。它在Chrome中完美运行,但在Mozilla FF或IE中无效。

这是我的代码,它在Chrome中运行良好,但在FF或IE中运行良好。

if (document.layers) { 
    document.captureEvents(Event.MOUSEDOWN); 
    document.onmousedown = function () { return false; }; 
} else { 
    document.onmouseup = function (e) { 
        if (e != null && e.type == "mouseup") { //Check the Mouse Button which is clicked. 
            if (e.which == 2 || e.which == 3) { //If the Button is middle or right then disable. 
                return false; 
            } 
        } 
    }; 
} 
document.oncontextmenu = function () { return false; };

1 个答案:

答案 0 :(得分:1)

试用此代码

function clickIE() {
    if (document.all) {  //document.all specific to Internet Explorer  
        return false;
    }
}
function clickAll(e) {
    if (document.layers || (document.getElementById && !document.all)) {  //document.layers specific to Netscape
        if (e.which == 2 || e.which == 3) {
            return false;
        }
    }
}
if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = clickAll;
} else {
    document.onmouseup = clickAll;
    document.oncontextmenu = clickIE;
}

document.oncontextmenu = new Function("return false")

DEMO