Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restricting the right-click context menu

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
This question was originally on the Browser forum but it has evolved into a Javascript question. Here's a summary of it:


I'm using a WebBrowser control within a VB application to display HTML documents, and I want to disable the Context Menu when the user right-clicks unless they have highlighted some text, in which case I want the context menu to appear.

In the code below when the user right-clicks on empty space, no context menu appears. If they highlight any text and right-click, they get the Cut / Copy / Paste etc menu.

To make this code perfect I just need a bit of help, because if the user then goes back and right-clicks on empty space they get the main context menu popping up - which is what I want to hide.

Code:
<HTML>

<HEAD>
<SCRIPT LANGUAGE="JavaScript">
var text = "";
function getActiveText(e) { 

text = (document.all) ? document.selection.createRange().text : document.getSelection();

if (text != '') document.body.oncontextmenu='return true';

}

document.onmouseup = getActiveText;
if (!document.all) document.captureEvents(Event.MOUSEUP);
</script>
</HEAD>

<BODY oncontextmenu='return false'>
<P>This is a big test!</P>
</BODY>
</HTML>

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
This is untested, but my guess would be

Code:
<HTML>

<HEAD>
<SCRIPT LANGUAGE="JavaScript">
var text = "";
function getActiveText(e) { 

text = (document.all) ? document.selection.createRange().text : document.getSelection();

if (text != '') document.body.oncontextmenu='return true';
else document.body.oncontextmenu='return false';
}

document.onmouseup = getActiveText;
if (!document.all) document.captureEvents(Event.MOUSEUP);
</script>
</HEAD>

<BODY oncontextmenu='return false'>
<P>This is a big test!</P>
</BODY>
</HTML>

-----------------------------------------
I cannot be bought. Find leasing information at
 
Yes, I also tried that because it seemed logical, but in fact what happens is that you always get the context menu.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top