jQuery Right-click Plugin

Easily add right-click functionality and disable browser context menus with this lightweight jQuery plugin.

Contents

  1. Overview
  2. Compatibility
  3. Usage
  4. Demo
  5. Download
  6. Limitations
  7. Licensing & Terms of Use

Overview

This plugin enables you to use jQuery to attach events triggered by the right mouse button. Three right-click events are supported: click, mouseup, and mousedown. There is also a built-in method to disable browser context menus.

Note that for left-handed users the “right” mouse button is actually on the left. The use of “right” and “left” when referring to mouse buttons has become commonplace, therefore it is used as such in this article to prevent confusion.

Compatibility

This plugin has been tested in the following browsers:

It is worth mentioning that one cannot currently detect a right click nor disable the context menu in Opera 9 or below.

Usage

$(document).ready( function() {

    // Capture right click
    $("#selector").rightClick( function(el) {
        // Do something
    });
   
    // Capture right mouse down
    $("#selector").rightMouseDown( function(el) {
        // Do something
    });
   
    // Capture right mouseup
    $("#selector").rightMouseUp( function(el) {
        // Do something
    });
   
    // Disable context menu on an element
    $("#selector").noContext();

});

Note: #selector may be any valid jQuery selector.

Demo

Visit the demo page for a full-featured demonstration of this plugin.

Download

Current version: Version 1.0 (13 May 2008)

Limitations

Multiple Events

You cannot combine a rightClick event with a rightMouseUp event. Most browsers do not return a true click event for the right mouse button. Given that, the plugin simulates a true click by detecting the mousedown and mouseup events. If both occur consecutively on the same element, a click event is fired.

Opera Browsers

Since Opera doesn’t support any of the functionality provided by this plugin, it is best to provide alternative ways for your users to achieve the same results wherever right-click events are used.

Licensing & Terms of Use

This plugin is licensed under a Creative Commons License and is copyrighted ©2008 by Cory S.N. LaViska.

Comments

Hi Cory! Great plugin, I was looking for something exactly like this. Most of the other solutions offered context menus but yours is the best for me because it allows one to build on it. thanks

#1 John on Jun 20th, 2008

Hi,
I am not a regular reader of this site but right now it reached my bookmarks. :)
A very nice plug in.
Greetings from Romania.

#2 Mircea Zetea on Jul 12th, 2008

Cool. Thx :)

#3 Mik Shvets on Aug 6th, 2008

Hi Cory,

(I'm new to jquery but...) shouldn't the handler be passed the underlying event, rather than a reference to $(this) ? So as to be consistent with the existing jquery click functions?


Thanks,

Tim

#4 Tim on Aug 18th, 2008

@Tim: good insight Tim, I'll take a look at it soon and update the code as necessary.

#5 Cory S.N. LaViska on Aug 24th, 2008

Add a comment

Name*

Email*

Never, ever sold or spammed :)

Homepage

Comment*

Sorry, plain text only :(