• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3351
  • Last Modified:

block right click menu?

Hi,

Two pretty simple questions:

1) I have a javascript function handler for right mouse clicks. I call this function when the user right clicks a control I have. I'd like to block the default browser context menu from appearing, so I did something like this:

    function onRightClickMyControl() {
        alert("you right clicked, blocking default context menu!");
        document.oncontextmenu = function(){return false};
    }

Ok that works, but after it is called once, all subsequent right mouse clicks on the page still block the default context menu. I'd like to only block the menu when my handler is called, otherwise display the default context menu.



2) What do the javascript Def() and Undef() functions do? Are they just testing if the variable name passed has actually been defined/created yet?

Thanks
0
minnirok
Asked:
minnirok
  • 4
  • 3
  • 2
1 Solution
 
pvginkelCommented:
First answer:

<body oncontextmenu="alert('you right clicked, blocking default context menu!'); return(false);">

2nd: Don't know.
0
 
Pravin AsarCommented:
Here is little example which can help to Block and UnBlock default RMB (Right Mouse Button) Menu

 
<script language="javascript">
var fnPtr = null;
function Block () {
fnPtr = document.oncontextmenu;
document.oncontextmenu = function () { alert('OOPS'); return false;};
}
function UnBlock () {
document.oncontextmenu = fnPtr;
}
</script>
<form>
      <input type="button" onClick="Block();" value="Block R M B" title="Click to Block R M B menu">
      <input type="button" onClick="UnBlock();" value="UnBlock R M B" title="Click to Un Block R M B menu">
</form>
0
 
minnirokAuthor Commented:
Hi,

I just need the menu blocked when my function is called.

pravinasar, your example requires the user to manually choose to 'unblock' the menu, but I need it done 'automatically':

function MyRightClick() {
    // only block default context menu here, no where else!
}

// ...

// Now any other time the user right clicks, I should automatically be able to display normal browser context menu.

Am I misunderstanding?

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Pravin AsarCommented:

You want to block RMN for only few elements (such images) ?

0
 
minnirokAuthor Commented:
Yeah - just block for a certain image - click anyplace else, default context menu is fine.

Thanks!
0
 
Pravin AsarCommented:
You want to block RMB for only few elements (such images) ?

<script language="javascript">
var fnPtr = null;
function Block () {
fnPtr = document.getElementById('img1').oncontextmenu;
document.getElementById('img1').oncontextmenu = function () { alert('OOPS'); return false;};
}
function UnBlock () {
document.getElementById('img1').oncontextmenu = fnPtr;
}
</script>
<img id="img1" src="../images/1.gif" width="100" height="100">
<form>
      <input type="button" onClick="Block();" value="Block R M B" title="Click to Block R M B menu for img1">
      <input type="button" onClick="UnBlock();" value="UnBlock R M B" title="Click to Un Block R M B menu for img1">
</form>
0
 
Pravin AsarCommented:
When you specify

document.oncontextmenu , the scope is entire document, that is why all over the page RMB is blocked.

0
 
minnirokAuthor Commented:
Perfect, that does it.

Thanks!
0
 
pvginkelCommented:
Yes, and the body specifies the whole page so it does the same.

If you just want to target images, do this: If you want to add more items where you wish to disable the context menu, add more lines in the disable_context_menu_on_items.

<html>
  <head>
    <script>
    function disable_context_menu() {
      alert('Context menu disabled!');
      return false;
    }
   
    function disable_context_menu_on_items() {
      disable_context_menu_on_tags('IMG');
    }
   
    function disable_context_menu_on_tags(tagName) {
      var o = document.getElementsByTagName(tagName);
     
      for (i = 0; i < o.length; i++) {
        o[i].oncontextmenu = disable_context_menu;
      }
    }
    </script>
  </head>
  <body onload="disable_context_menu_on_items()">
    <img><br>
    <img><br>
    <img><br>
  </body>
</html>
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now