Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery/JS prevent right click menu in browsers

I have my div with a right click popup menu:

// Attatch right click event to folder for extra options
$('#fBox' + folderID).mousedown(function(event) {
    if (event.which == 3) {

        // Set ID
        currRClickFolder = folderID;

        // Calculate position to show popup menu
        var height = $('#folderRClickMenu').height();
        var width = $('#folderRClickMenu').width();
        leftVal = event.pageX - (width / 2) + "px";
        topVal = event.pageY - (height) + "px";
        $('#folderRClickMenu').css({ left: leftVal, top: topVal }).show();

    }
});

But the browser for this element still pops up the default menu (copy/paste/properties etc). Any way to disable this? I've tried return false but not luck.

like image 288
Tom Gullen Avatar asked Feb 07 '11 10:02

Tom Gullen


4 Answers

You can disable the right click by appending oncontextmenu="return false;" to your body tag.

<body oncontextmenu="return false;"> 
like image 194
Arseny Avatar answered Oct 08 '22 15:10

Arseny


You can disable context menu on any element you want:

$('selector').contextmenu(function() {     return false; }); 

To disable context menu on the page completely (thanks to Ismail), use the following:

$(document).contextmenu(function() {     return false; }); 
like image 35
Arsen K. Avatar answered Oct 08 '22 15:10

Arsen K.


One jQuery line:

$('[id^="fBox"]').on("contextmenu", function(evt) {evt.preventDefault();});
like image 42
KrisWebDev Avatar answered Oct 08 '22 16:10

KrisWebDev


Try this:

$('#fBox' + folderID).bind("contextmenu", function () {
                alert("Right click not allowed");
                return false;
            });
like image 45
Sang Suantak Avatar answered Oct 08 '22 15:10

Sang Suantak