Join us at Facebook
jQuery and ASP.NET

November 28, 2009

Disable Right Click Easily using jQuery




I’ve seen allot of posts on forums on how to disable the right click event in JavaScript. Well the solution is pretty straight forward if you use jQuery! The trick is to bind to the contextmenu event. The code below is an example of how to do this.

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head>
<
title>jQuery disable right click easily</title>
<
script language="javascript" type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</
script>
<
script type="text/javascript" language="javascript">
$(function() {
$(this).bind("contextmenu", function(e) {
e.preventDefault();
});
});
</script>
</
head>
<
body>
Sed lacus. Donec lectus. Nullam pretium nibh ut turpis.
Nam bibendum. In nulla tortor, elementum vel, tempor at,
varius non, purus. Mauris vitae nisl nec consectetuer.
Donec ipsum. Proin imperdiet est. Phasellus dapibus semper
urna. Pellentesque ornare, orci in consectetuer hendrerit,
urna elit eleifend nunc, ut consectetuer nisl felis ac diam.
Etiam non felis. Donec ut ante. In id eros.
</body>
</
html>



See a Live Demo


Bookmark this link on del.icio.us (saved by 0 users)

Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter

comments

10 Responses to "Disable Right Click Easily using jQuery"
  1. Anonymous said...
    November 30, 2009 6:17 AM

    I personally don't think it is a good idea to do this. Right-Click is an established and accepted user behavior, it is not our place as developers to limit this type of action, if anything we should only try to enhance these aspects of the user experience.

  2. Mike Heath said...
    November 30, 2009 8:24 AM

    I totally agree with anonymous. Why would you want to disable the context menu in the first place. I thought we got over this stupid practice on the web years ago.

  3. Anonymous said...
    November 30, 2009 10:59 AM

    Great Snippet !! Even now context is being disabled for right click in many of banking and other applications. Yes Developers love to give freedom but not at the cost of application security.

    Thanks for the snippet.. once again

  4. gdlsystems said...
    December 9, 2009 8:08 AM

    Great simple solution! I totally agree with Anonymous in the fact that we should not disable context menu since it is an standard way of using the web. However there are customers that even when they are told to not do so they still want to disable the context menu.

  5. firehose911 said...
    January 4, 2010 11:34 AM

    How to enable it back again? In my case I'm using jQuery (plug-in) context menu on right click for grid options. When cliced on a new row, the original menu should come back for the rest and jQuery contextMenu shuoud be displayed only for the most recent right click row.

  6. Malcolm Sheridan said...
    January 4, 2010 2:20 PM

    @firehose911
    You could try bindind to specific elements instead of $(this). Try that.

  7. who me??? said...
    April 21, 2010 11:45 PM

    i just need to disable the right click when a user right clicks on the page and does a refresh.

    But when i use the above code it disables all the right clicks even the ones on the text boxes. which only shows cut copy paste Delete and select all..

    any idea if i can retain those and only disable the main Context menu of just the page.

  8. Anonymous said...
    May 10, 2010 3:53 AM

    I partially agree with Anon, there are a lot of web driven applications where disabling right click has its benefits. However, For standard websites the context menu has its place.

  9. sadettin said...
    August 1, 2010 2:49 AM

    tnx my friend good work ;)

  10. Muhammad Taqi said...
    August 17, 2010 3:19 AM

    jQuery and mootools

    i love both :)

 

Copyright 2010 All Rights Reserved DevCurry.com by Suprotim Agarwal