Epic Voyage logo

Epic Voyage

The Wanderer's Journal

Main menu

  • Home

Geek stuff: Disable right-click on website images

One of my customers wanted to prevent people from right-clicking on images on their website. That brought back memories of websites on GeoCities a decade ago. There are still so many ways around it that the attempt is almost useless. My favorite way was always to view the page source. It is also possible to disable JavaScript, use a website debugging plugin, or save the (complete) page to a directory on your harddrive. In the past, the popular method to stop image theft has been to make a JavaScript Alert() box pop up when the user right-clicked on a picture. This quickly makes the website look less professional. With Web 2.0 being so popular, there are better ways now to hide the context menu. Luke Breuer has a tutorial on how to replace the browser’s default with your own. After looking at his code, it looks like the code required to silently prevent right-clicking on images is surprisingly simple:

<script language="JavaScript" type="text/javascript">
function ContextHide(event) {
  if (event == null)
    event = window.event;

  var target = (event.target != null) ? event.target : event.srcElement;

  if (target.tagName.toLowerCase() == 'img')
    return false;
}

document.body.oncontextmenu = ContextHide;
</script>

Note that I intentionally did not include any comments. Go read up on what all of this does. It works on Firefox 3, Internet Exploder 7, Safari 4 (Public Beta), and Google Chrome 2. It does not seem to work with Opera, even after enabling the ability for scripts to see right-click events (non-default setting). Enjoy.

Overheard at the Tavern

To pass lightly from old laws to new ones is a certain means to weakening the inmost essence of all law whatever. — Aristotle
Another »

Since This Page Loaded...

0 million dollars have been spent by Washington.
0.00 dollars per person in the US.

And that is only Federal spending...

0 babies have died worldwide in an abortion.
0 babies were in the United States.

0 Americans have contracted an STD.

About Me

{author}
Chris is probably out getting lost somewhere. He has a tendency to do that. Please don't worry unless he fails to show up again sometime in the next week.

Copyright 2024. All rights reserved.