Link to home
Start Free TrialLog in
Avatar of minnirok
minnirok

asked on

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
Avatar of pvginkel
pvginkel
Flag of Netherlands image

First answer:

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

2nd: Don't know.
Avatar of Pravin Asar
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>
Avatar of minnirok
minnirok

ASKER

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

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

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

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When you specify

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

Perfect, that does it.

Thanks!
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>