Disabling the Context Menu - DHTML Lab - DHTML Diner | 5 | WebReference

Disabling the Context Menu - DHTML Lab - DHTML Diner | 5

DHTML Diner Logo

Disabling the Context Menu



All Together Now!

To disable the context menu for as many instances as possible in a page, in as many browser versions and platforms as allowable, we can use the following:

document.oncontextmenu = function(){return false}
if(document.layers) {
    window.captureEvents(Event.MOUSEDOWN);
    window.onmousedown = function(e){
        if(e.target==document)return false;
    }
}
else {
    document.onmousedown = function(){return false}
}
We do not need much in the way of browser sniffing, so the code remains fairly clean:

document.oncontextmenu =
     function(){return false}
- disables menu for IE5 Windows for all elements
- code not harmful to other browsers
if(document.layers) {
    window.captureEvents(Event.MOUSEDOWN);
    window.onmousedown = function(e){
        if(e.target==document)return false;
    }
}
- disables menu for Navigator 4.x, except for links, images and other elements that respond to mousedown.
- code will cause errors in other browsers, so a simple browser sniff for NS 4.x using if(document.layers) is used.
else {
    document.onmousedown =
         function(){return false}
}
- disables menu for IE Macintosh for all elements.
- code is hidden from NS by else statement
- not harmful to IE for Windows.

Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: April 28, 2000
Revised: April 28, 2000

URL: http://www.webreference.com/dhtml/diner/contextmenu/5.html