JQuery right click (contextmenu event)
The
solution is very simple if we use jQuery. You need to bind to the contextmenu
event. Contextmenu is a javascript event that is triggered when the user right
clicks on an element. It’s is not a custom jQuery event.
The Code below is an example of how to do this.
$('element').bind('contextmenu', function(e){
e.preventDefault();
//code goes here
});
Disables right-click with JQuery:
$('element').bind('contextmenu', function
(e) {
return false;
});
To change the link of a page while right-click:
$("a.class-name").bind("contextmenu", function
(e) {
var href = $(this).attr("href").toString();
$(this).attr("href",
"http://" +
document.domain.toString() + "/somepage.aspx");
});
Example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery right click example</title>
<script src="Scripts/jquery-1.4.1.min.js"
type="text/javascript"></script>
<script type="text/javascript"
language="javascript">
$(function () {
$(".rightClick").bind("contextmenu", function
(e) {
e.preventDefault();
});
$("a.lnkrgtClick").bind("contextmenu", function
(e) {
var href = $(this).attr("href").toString();
$(this).attr("href", "http://"
+ document.domain.toString() + "/somepage.aspx");
});
});
</script>
</head>
<body>
<div style="background-color:#ccc; border:1px solid;">
Simple div with right-click event</div>
<br />
<div class="rightClick"
style="background-color:#D7DDDD; color: #4D0202; border:1px solid;">
This
is the example of disables right-click with JQuery</div>
<br />
<a href="testpage.aspx"
class="lnkrgtClick">This
is the example of to change the link
while
right-click</a>
</body>
</html>