Jul 5 2012

Disable Right Click Menu in HTML

When browsing a webpage, you typically can click the right button on the mouse to reveal a context menu. The right click context menu usually has options such as Back, Forward, Reload, Stop, and View Page Source. When right clicking on an image, you have have the option to save the image or even set it as the desktop background. It’s rare but sometimes you want to disable the right click context menu. To disable the right click context menu you can set the oncontextmenu event to return false.

<body oncontextmenu="return false;">

If you only want to disable the right click context menu on a small part of your site, for example an image you don’t want your visitors to easily download or save you can add the oncontextmenu property to any div or img tag and disable the context menu only in that HTML tag.


Sep 24 2005

Hide Your HTML Source

Sometimes you might not want to allow guests to your website to view the HTML source code. You can disable the right click context menu by using the following bit of code in your page:

<body oncontextmenu='return false;'>

You can use this feature to prevent users from right clicking and saving an image from your site. This is support by IE and Firefox. I have seen sites use similar functionality to this to pop up an alert with a copyright message is a user tries to right click on an image.

Keep in mind that a user can always disable JavaScript altogether, view the source from the application menu, and/or even save the whole HTML file onto his/her desktop and view the source from Notepad.