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
LVL 7
minnirokAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pvginkelCommented:
First answer:

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

2nd: Don't know.
0
Pravin AsarPrincipal Systems EngineerCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Pravin AsarPrincipal Systems EngineerCommented:

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 AsarPrincipal Systems EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pravin AsarPrincipal Systems EngineerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.