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?
|
|
|
||
|
|
|
|
|
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |






comments
10 Responses to "Disable Right Click Easily using jQuery"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.
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.
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
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.
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.
@firehose911
You could try bindind to specific elements instead of $(this). Try that.
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.
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.
tnx my friend good work ;)
jQuery and mootools
i love both :)
Post a Comment