PDA

View Full Version : No Right Click II (on images) doesn't work in Firefox



Radiophile
10-20-2005, 05:00 PM
I believe I dutifully followed the instructions for adding this script to my Web page, but it doesn't work properly in Firefox. I have FF version 1.07.

What happens is, when I right click the image, I get the pop-up dialog box as I should, but when I click OK to dismiss it, I get the very right-click menu that I would have gotten had the script not been present in the code. That is, I get the right-click menu for images, the one that contains the command "Save Image as..." among others.

If I try this on the DD demo page for this script, it works correctly. Mine is a very simple Web page, and I don't know what could be wrong. Please have a look at my html and tell me!

Thanks. Here's my test Web page: http://www.radiophile.com/ytr601dd.htm

jscheuer1
10-21-2005, 05:11 AM
Does the same thing here for me too. Even the demo does the same thing if I drag the OK button on the alert over the image before clicking OK. Just out of curiosity, how many buttons does your mouse have? Mine only has two and it looks like the script is set up to key off of the 3rd button on a three button mouse in FF but off of button 2 in IE. I really wouldn't worry about it though as, in any browser you can disable such scripts simply by turning off javascript.

Radiophile
10-21-2005, 12:38 PM
A-ha! You're right -- it depends on the position of the OK button when you click it to dismiss the dialog box. If it's over an image, you get the Images menu; if it's not over an image, you don't. The demo page for the script is (conveniently!) formatted so the dialog box isn't over the image, so the script "works" on that page.

I'm aware that these scripts, even if working, are easy to work around. Working around them does, however, require an extra step or two, and I'm convinced that's all that's needed to discourage the majority of plagiarism. (And to answer your question: I have a two-button mouse -- don't the vast majority of PC users? If it works correctly with a three-button mouse that's little comfort.)

Thanks for figuring out why it works on the demo page and not on mine. Almost all my images are centered on the page, so I was really scratching my head about why it didn't work on every one of my pages I tested. I think this is a real bug in the script; I wonder if there's any way to fix it?

jscheuer1
10-22-2005, 07:32 AM
Try out this version, works here:


<script type="text/javascript">

/*
Disable right click script II (on images)- By Dynamicdrive.com
For full source, Terms of service, and 100s DTHML scripts
Visit http://www.dynamicdrive.com
Modified here to disable IE image hover menu and
truly disable right click in FF by jscheuer1 in
http://www.dynamicdrive.com/forums
*/

var clickmessage="Right click disabled on images!"

function disableclick(e) {
if (document.all) {
if (event.button==2||event.button==3) {
if (event.srcElement.tagName=="IMG"){
alert(clickmessage);
return false;
}
}
}
else if (document.layers) {
if (e.which == 3) {
alert(clickmessage);
return false;
}
}
else if (document.getElementById)
if (e.which==3&&e.target.tagName=="IMG")
setTimeout("alert(clickmessage)",0)
}

function associateimages(){
for(i=0;i<document.images.length;i++)
document.images[i].onmousedown=disableclick;
}

if (document.all){
document.onmousedown=disableclick
for (var i_tem = 0; i_tem < document.images.length; i_tem++)
document.images[i_tem].galleryimg='no'
}
else if (document.getElementById)
document.onmouseup=disableclick
else if (document.layers)
associateimages()
</script>

Place it at the same location, just above the closing </body> tag. I also added language that disables the IE image bar.

Radiophile
10-22-2005, 06:16 PM
John,

Zowie! It works! Thanks very much. Thanks also for including language to disable the IE image toolbar, too. It's up and running, together with the No Select Text script, on my test page http://www.radiophile.com/ytr601dd.htm .

I wonder...should I report this on the Bug Reports forum?

John

Twey
10-22-2005, 07:22 PM
If you go to Preferences=>Web Features and click the "Advanced" button next to "Enable Javascript," there is an option to allow/disallow scripts to disable/replace context menus.

And, yes.

jscheuer1
10-23-2005, 03:45 PM
If you go to Preferences=>Web Features and click the "Advanced" button next to "Enable Javascript," there is an option to allow/disallow scripts to disable/replace context menus.

True but, in this case, it has no effect on this script as it technically does not replace or disable the context menu. Rather it launches the alert so soon after the context menu appears that the context menu, although very breifly shown, is rendered useless.

Twey
10-28-2005, 10:37 PM
Here I find it the other way around - the alert is shown, then the context menu pops up as soon as it closes. This happens with all "no right click" scripts.

eamondo
10-28-2005, 11:54 PM
Why bother instaling this script when visitors can just press print screen

jscheuer1
10-29-2005, 05:49 AM
Here I find it the other way around - the alert is shown, then the context menu pops up as soon as it closes. This happens with all "no right click" scripts.

That was the behavior of the script under XP/FF before my modifications, did you test this with my modifications? Both I and the OP found they did the trick.


Why bother instaling this script when visitors can just press print screen

I'm with you eamondo, I just get carried away sometimes with whatever the OP wants. It will discourage some people. I think that was all the OP wanted.

Twey
10-29-2005, 09:27 AM
You are right, I was referring to no-right-click scripts in general, not this one, which has obviously been crafted especially for the situation.

jscheuer1
10-30-2005, 03:56 AM
You are right, I was referring to no-right-click scripts in general, not this one, which has obviously been crafted especially for the situation.

Actually, it should work in just about any situation in which an alert is desired. I think I've hit upon a vulnerability, if you will, in FF. By setting a 0 timeout for the alert, it doesn't technically replace or disable the context menu. It just gives the browser something else to do so soon after the context menu appears, and since the default behavior of an alert in FF is to end any context menu open when the alert fires ergo, virtually no context menu.

rob-y2k
06-28-2009, 11:45 AM
I love the no right click script you wrote Jscheuer1, thanx.

I'm having a problem with the W3C Markup Validation Service. Its bring up about 14 errors and warnings.

Can anyone help with this please.

Thanx Rob..

sdm.test
11-15-2009, 04:02 AM
Hi,

this code was very helpful but can someone tell me how to disable right click for a single image even though we have multiple images on the page?

What modifications should be made to the above code?

Thanks.

jscheuer1
11-15-2009, 04:58 AM
I love the no right click script you wrote Jscheuer1, thanx.

I'm having a problem with the W3C Markup Validation Service. Its bring up about 14 errors and warnings.

Can anyone help with this please.

Thanx Rob..

Make the script external:


Use a text editor to save the script, call it 'file_name.js' where 'file_name' can be any valid file name of your choosing. Substitute the name of your external .js file for some.js in the below:


<script src="some.js" type="text/javascript"></script>

Common problems arise when:

1 ) The script file is not in the directory specified. In the above example it must be in the same directory as the page(s) that use it. Below, it can be in the scripts directory off of the root of a domain:


<script src="http://www.somedomain.com/scripts/some.js" type="text/javascript"></script>

2 ) Opening, closing and/or 'hiding' tags are left in the external file. This means that you must strip:
<script>
<!--and
//-->
</script>and any of their many variations from the beginning and end of the external file.

3 ) The external call (<script src="some.js" type="text/javascript"></script>) is not inserted into the page at the correct spot. The external call must be inserted at the same place on the page where the script was/would have been.

4 ) Paths to other files (if) used by the script are no longer valid due to its location. This is only a problem if the external script is kept in a different directory than the page it was working on when it was an internal script. To correct this, use absolute paths inside the script. Absolute path examples:


http://www.somedomain.com/images/button.gif

http://www.somedomain.com/~mysitename/index.html

5 ) Inappropriately combining two or more scripts into one external file. Usually external scripts can be combined if one knows enough about scripting to do so properly. Even then it is possible to overlook something.

A rule of thumb when testing is, if it won't work on the page, it won't work as an external file either.

One other thing, if this is a DD script or any script that requires the credit remain for legal use, include the credit in the on page call, ex (see usage terms (http://www.dynamicdrive.com/notice.htm) for more info on this, especially item 4 from the terms):


<script src="some.js" type="text/javascript">
/***********************************************
* IFrame SSI script II- Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of original DHTML scripts
* This notice must stay intact for legal use
***********************************************/
</script>

Make sure to retain all the 'decorations', as these include begin and end javascript comment delimiters, without which the script won't function.

There is also info here:

http://www.javascriptkit.com/javatutors/external.shtml

jscheuer1
11-15-2009, 05:12 AM
Hi,

this code was very helpful but can someone tell me how to disable right click for a single image even though we have multiple images on the page?

What modifications should be made to the above code?

Thanks.

In the script there are two places where it identifies the element clicked upon as an image:


if (event.srcElement.tagName=="IMG"){

and:


if (e.which==3&&e.target.tagName=="IMG")

If you substitute an identification for the image in question, it will act the way you want. There are various ways of going about that. The simplest to explain would be to give the image you are wanting to single out a unique id, ex:


<img id="noClick" src="special_image.jpg" alt="">

Then change the first line to:


if (event.srcElement === document.all['noClick']){

and the second line to:


if (e.which==3&&e.target === document.getElementById('noClick'))