By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,686 Members | 2,770 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

Disable right-click

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Dennis Allen" <de****@dennisallen.com> writes:
Hi. What's the best way to display an image, disabling the user's ability
to right-click on it?
In general, no. (Not even disabling the effect of right-clicking :).

Some browsers will allow it, but not all, and it is trivial to circumvent.
I believe you have to use <body oncontextmenu="return false">.
That will, in some browsers, disable the context menu for the entire
page. Highly annoying a reason enough to leave the page.

You can put it on the image itself instead:
<img oncontextmenu="return false;" src="..." alt="...">
(remember, the "alt" tag is *required* for valid HTML).
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()


Have you tried? What was the result?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
In article <10*************@corp.supernews.com>, de****@dennisallen.com
says...
Hi. What's the best way to display an image, disabling the user's ability
to right-click on it?


None.

--
Hywel I do not eat quiche
http://hyweljenkins.co.uk/
http://hyweljenkins.co.uk/mfaq.php
Jul 20 '05 #3

P: n/a
Strangely enough, it did work. Use an <a href="temp.jpg"
onclick=Display(this)>, which does:

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

Of course this doesn't disable the file "Save As" for the web page. IE
saved the "Display()" web page, but not the image itself. Is that because
oncontent is disabled?

As for why I need to do it, the client requested it...Dennis

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:4q**********@hotpop.com...
"Dennis Allen" <de****@dennisallen.com> writes:
Hi. What's the best way to display an image, disabling the user's ability to right-click on it?
In general, no. (Not even disabling the effect of right-clicking :).

Some browsers will allow it, but not all, and it is trivial to circumvent.
I believe you have to use <body oncontextmenu="return false">.


That will, in some browsers, disable the context menu for the entire
page. Highly annoying a reason enough to leave the page.

You can put it on the image itself instead:
<img oncontextmenu="return false;" src="..." alt="...">
(remember, the "alt" tag is *required* for valid HTML).
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()


Have you tried? What was the result?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #4

P: n/a

"Dennis Allen" <de****@dennisallen.com> wrote in message
news:10*************@corp.supernews.com...

[right click disable]
As for why I need to do it, the client requested it...Dennis


Demonstrate to your client how easy it is to left click on an image and drag
it to your pictures folder.

Cheers
Richard.
Jul 20 '05 #5

P: n/a
Here's How I do it.

The code below in in a file called 'DisableRClick.js'
Every page has :
<script language="Javascript" type="text/javascript"
src"include/disableRClick.js"></script>
in its header

DisableRClick.js contains the code:

document.oncontextmenu = function(){return false}
if(document.layers) {
window.captureEvents(Event.MOUSEDOWN);
window.onmousedown = function(e){
if(e.target==document)return false;
}
}
else {
document.onmousedown = function(){return false}
}

This works in IE and Mozilla - havn't test with Netscape yet.

Steve

"Dennis Allen" <de****@dennisallen.com> wrote in message
news:10*************@corp.supernews.com...
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

Jul 20 '05 #6

P: n/a
Steve wrote:
Here's How I do it.

The code below in in a file called 'DisableRClick.js'
Every page has :
<script language="Javascript" type="text/javascript"
src"include/disableRClick.js"></script>
in its header

DisableRClick.js contains the code:

document.oncontextmenu = function(){return false}
if(document.layers) {
window.captureEvents(Event.MOUSEDOWN);
window.onmousedown = function(e){
if(e.target==document)return false;
}
}
else {
document.onmousedown = function(){return false}
}

This works in IE and Mozilla - havn't test with Netscape yet.


No it doesn't, you just *think* it "works" there.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #7

P: n/a
In article <10*************@corp.supernews.com>, de****@dennisallen.com
enlightened us with...

As for why I need to do it, the client requested it...Dennis

You might want to mention to your client that disabling the right click
menu violates the accessibility rules.
Disabled people use that menu to navigate. Some of us who aren't
disabled use it, too.
Disabling it is irritating, to say the least.

And any idiot can drag and drop an image from IE.
Any moron with an image editor can screen cap the page and edit the
image. Slightly more IQ points and they can remove your digital
watermark.
Anyone with a moderate level of intelligence can read the source, get
the link, and view it as an image by typing it in the address bar.

Anyone can disable javascript and bypass anything you do with it.

Sometimes, we need to save clients from themselves. heh

--
--
~kaeli~
A plateau is a high form of flattery.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #8

P: n/a
1st:

use the following code to disable right-click-period.
I got it from DynamicDrive.com:

<script language=JavaScript>
<!--

//Disable right mouse click Script
//By Maximus (ma*****@nsimail.com) w/ mods by DynamicDrive
//For full source code, visit http://www.dynamicdrive.com

var message="Function Disabled!";

///////////////////////////////////
function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!docume nt.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("alert(message);return false")

// -->
</script>

Then add this to your META Tag section up in your Page's Head section:

<meta http-equiv="imagetoolbar" content="no">
<meta http-equiv="imagetoolbar" content="false">


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #9

P: n/a
eric garcia wrote:
1st:

use the following code to disable right-click-period.
No. You use that worthless, non-working garbage to attempt to disable
it. It does *not* disable the right click.
I got it from DynamicDrive.com:
Give it back. Its useless.

<--snip-->
Then add this to your META Tag section up in your Page's Head section:

<meta http-equiv="imagetoolbar" content="no">
<meta http-equiv="imagetoolbar" content="false">


That does nothing in my browser. But even in IE6, I don't even need to
use my mouse to get anything in the page. File>Save As and there it is.
Every bit of it.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.