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
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
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>
<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>
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
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) ?
ASKER
Yeah - just block for a certain image - click anyplace else, default context menu is fine.
Thanks!
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
When you specify
document.oncontextmenu , the scope is entire document, that is why all over the page RMB is blocked.
document.oncontextmenu , the scope is entire document, that is why all over the page RMB is blocked.
ASKER
Perfect, that does it.
Thanks!
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_it ems.
<html>
<head>
<script>
function disable_context_menu() {
alert('Context menu disabled!');
return false;
}
function disable_context_menu_on_it ems() {
disable_context_menu_on_ta gs('IMG');
}
function disable_context_menu_on_ta gs(tagName ) {
var o = document.getElementsByTagN ame(tagNam e);
for (i = 0; i < o.length; i++) {
o[i].oncontextmenu = disable_context_menu;
}
}
</script>
</head>
<body onload="disable_context_me nu_on_item s()">
<img><br>
<img><br>
<img><br>
</body>
</html>
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_it
<html>
<head>
<script>
function disable_context_menu() {
alert('Context menu disabled!');
return false;
}
function disable_context_menu_on_it
disable_context_menu_on_ta
}
function disable_context_menu_on_ta
var o = document.getElementsByTagN
for (i = 0; i < o.length; i++) {
o[i].oncontextmenu = disable_context_menu;
}
}
</script>
</head>
<body onload="disable_context_me
<img><br>
<img><br>
<img><br>
</body>
</html>
<body oncontextmenu="alert('you right clicked, blocking default context menu!'); return(false);">
2nd: Don't know.