PDA

View Full Version : How to capture right click and disable context menu?



afru
02-09-2005, 06:40 PM
Problem : How to capture right click from inside <a> tag or <img > tag through onmousedown or onmouseup events and disable context menu?

Below is the code I have tried to implement.



<SCRIPT LANGUAGE="JavaScript">
<!--

function myhandler(e)
{
//// do something with event obj......
return false;
}

//-->
</SCRIPT>

<a href="http://www.yahoo.com" onmousedown="return myhandler(event);"
onmouseup="return myhandler(event);"
onclick="return myhandler(event);">
Right click should be disabled on this item.</a>



I want to disable right click only on particular links or images. So I wrote the above code. What is wrong with above code. Why does the browser not responding to return false; I just want to capture mouse events and use them as per my purpose and then disable them all by returning false.

I think onclick is working fine and so the browser is not going to yahoo.com But when you right click, none of the code is working. It is displaying right context menu of the browser.

I know how to add event listener to a particular ID. But what I dont want to write code for particualr IDs. I want to capture right click event from inside of <a href> tag or <img> tag where ever I need it and then disable right context menu.

Hope somebody will work out what is wrong with above code.

A working cross browser example will be fabulous.

Thanking you.

Afru.

cr3ative
02-09-2005, 07:01 PM
A working cross browser example will be fabulous.Correction: Impossible.

It's never a good idea to use these kinds of scripts. If you put information on the internet, there is always a way to get that information. You can never make information secure, which renders all scripts of this sort useless. They also highly infuriate users of all types.

cr3ative

afru
02-09-2005, 07:08 PM
Sorry to say that I dont need this script to secure pages. I agree with your saying that there is always a way to get that information.

My idea is to have different right click javascript menus for different elements or links on the site.

So that you can create right click menu for images, links and even table cells. I know how to write it by using add listener to document but it will make only one menu for the whole document. I think you understand what I am upto.

If you have a bit time, can you please post how to check event object in the handler function to disable default context menu of the browser.

Thanking you,

Afru.