PDA

View Full Version : Disabling right-click


dallen24
02-17-2004, 11:03 PM
Hi. What's the best way to display an image, disabling the user's ability to right-click on it?

I believe you have to use <body oncontextmenu="return false">. Would the following work?

var x = "<head><\/head><body oncontextmenu="return false"><img src='temp.jpg' border=0><\/body>"

top.document.open()
top.document.write(x)
top.document.close()

Since this is a frameless site, I don't think this would work, at least would never get to the close()...Dennis

Nightfire
02-17-2004, 11:15 PM
Before you waste time disabling stuff, give up now. You can't stop people getting your images.

brothercake
02-17-2004, 11:20 PM
Not only can you not prevent it, but you're introducing accessibility problems by trying. The right click does a lot more than "save as" - what if I want to open a link in a new window, add it to my favourites, or send some highlighted text to Google?

Garadon
02-17-2004, 11:25 PM
<img oncontextmenu="return false;">

but listen to all the resons why not to do it :).

Philip M
02-18-2004, 07:21 AM
This rather repetitive show is already running at

http://www.codingforums.com/showthread.php?s=&threadid=33485

The short answer is:

a) you cannot protect your code or images from someone who is technically proficient and is determined to capture (steal) them.

b) you can protect your code and images against people who have no knowledge of Javascript, which in fact is pretty well everyone.

Even if the protection is of limited value it still makes it clear to the guy who is trying to capture your image that you do not permit him to do so and hence he must realise that he is a thief. I do appreciate that in this day and age many people are not too bothered by this appellation.

Philip M
02-18-2004, 07:31 AM
Originally posted by brothercake
Not only can you not prevent it, but you're introducing accessibility problems by trying. The right click does a lot more than "save as" - what if I want to open a link in a new window, add it to my favourites, or send some highlighted text to Google?

Here's a tip you may not have discovered - try clicking on that thing like two overlapping squares in the top right-hand corner of the screen, which makes it shrink down a bit and reveals a menu bar. Then you can select "Add to Favorites" and so on.

Having said that, I am not too clear why you want to add to your favourites a site which you say you will never visit again because it offends you. Possibly a folder called "Abhorrances" or something?

Roy Gardiner
02-18-2004, 11:00 AM
Originally posted by Philip M
a) you cannot protect your code or images from someone who is technically proficient and is determined to capture (steal) them.

b) you can protect your code and images against people who have no knowledge of Javascript, which in fact is pretty well everyone You don't have to be technically proficient (I'm not) neither do you have to know Javascript. You simply have to be able to find the image in the Temporary Internet Files folder. No problem. Disabling the right-click is simply annoying and shows that the author is ignorant if he thinks he's protecting stuff.

Roy Sinclair
02-18-2004, 02:45 PM
There is a technique which can be used to protect your images as well as a no-right-click script (that is to say, against those who don't know very much) but doesn't involve messing with the user interface.


<img src="transparentpixel.gif" style="height: 100px; width: 200px; background-image: url(myrealimage.jpg);" />


Anyone right-clicking on the image and saving it will get the transparent pixel gif image. Of course the user could use the "Save Background As" menu item, delve the local cache examine the markup and directly download the image etc just like anyone with the knowledge of how to bypass a no right click script but it will provide protection against the same people who don't know how to bypass the no right click script and won't upset those who don't like such scripts.

Philip M
02-18-2004, 06:59 PM
Originally posted by Roy Gardiner
You don't have to be technically proficient (I'm not) neither do you have to know Javascript. You simply have to be able to find the image in the Temporary Internet Files folder. No problem. Disabling the right-click is simply annoying and shows that the author is ignorant if he thinks he's protecting stuff.

Well, as I said before:

Even if the protection is of limited value it still makes it clear to the guy who is trying to capture your image that you do not permit him to do so and hence he must realise that he is a thief. I do appreciate that in this day and age many people are not too bothered by this appellation.

Willy Duitt
02-18-2004, 07:09 PM
Or you can try to force/trick the user into agreeing to abide to a ridiculous TOS like this. :rolleyes:

Use of this Web site constitutes acceptance of the User Agreement (http://www.cprr.org/Museum/legal.html)

.....Willy

BTW: How's that for a TOS? Legally binding?

Philip M
02-18-2004, 07:37 PM
I think it is supposed to be a joke, Willy.
Tongue in cheek, you know? A sort of parody.
Probably intended to make you laugh.

Willy Duitt
02-18-2004, 07:44 PM
That's not a joke. The guy is serious!
If he has the resources to file a suit is another matter.

.....Willy

brothercake
02-18-2004, 07:56 PM
Originally posted by Philip M
Having said that, I am not too clear why you want to add to your favourites a site which you say you will never visit again because it offends you. Possibly a folder called "Abhorrances" or something?
Well I wouldn't ... and yeah there are ways to do its functions even it's not there ... but the point is the context menu is mine, it doesn't belong to the site I'm visiting. Websites have no right interfering with it.

Garadon
02-18-2004, 08:11 PM
Out of my curiosity, some(many) on this site keep saying websites have no rite interferrring. yada yada etc....


anyway I was wondering since you guys so LIKE clear borders between the sites area and the clients what your stands on:

-pop up blocking(user blocking a feature that does not effect his user interface.)
-IMAGE/ADD blocking(user blocking a part of a site clear stepping beyond the bounds of his user interface.)

SlySecretSpy
02-18-2004, 08:20 PM
Here is my proposal to protect images, it would take a bit of coding, but should protect even against those with technical abilities.

You would need to store the images in a DB or on a part of the filesystem that is not accessable to the web.

You would need to do this in a language other then Javascript as Javascript is not powerfull enough.

Here is how it would work. When a page is loaded an encrypted key is generated at the start of the page.

The images would be refferenced via a script that would send the requested image id as well as the encrypted key. The script would then access the db and return the image and append the key to a field associated with the image's record. The ecrypted key would contain a time stamp as well, so the key would expire after 15 seconds, however if it is refferenced a second time within 15 seconds, it will return a blank image.

The key field would be checked for old keys every time the image is requested.


The script would contain code to prevent against the browser from caching the file, so if somone did a save as, when they requested the file from the server again they would be downloading a blank image.

JAVAEOC
02-18-2004, 11:00 PM
the only time you should disable rightclick is when YOU actually provide a rightclick menu (even though this alos might not be good for the user so you should always give him a choice)

like a hidden rightclick menu by
SlySecretSpy (I think)

dallen24
02-18-2004, 11:22 PM
Thanks for the replies. My example was just off the top of my head, but it woked:

<a href src="temp.jpg" onclick="return Display(this)">click</a>

function Display(obj) {
var img = obj.getAttribute("href")
var x = '<head><\/head><body ondrag="return false" oncontextmenu="return false"><img alt="" src="'+img+'" border="0"><\/body>'
top.document.open()
top.document.write(x)
top.document.close()
return false

Added ondrag= to prevent people from dragging the image to the desktop.

This method doesn't disable the file->save as, but I tried it and I didn't see any jpg file saved. Only saw the htm file with the above code.

Yes, a lot of anger against disabling right-click. I don't want to do it either, but it's what the client wants...Dennis

Garadon
02-19-2004, 12:05 AM
You can't protect images symply cause all keyboards are equiped with a print screen button

JAVAEOC
02-19-2004, 12:19 AM
Well, if the client wants that and disabaling the rightclick means money for you, i would say go ahead and do it :)

have fun coding

oldcrazylegs
02-27-2004, 08:17 PM
Most people don't want something until you tell them they can't have it. Then they will do anything to get it. :)