Solved

Suppress Right Click in Firefox

Posted on 2008-10-16
8
573 Views
Last Modified: 2011-10-19
We want to disallow a right click (to download pictures) from our web site.

We are using this JavaScript code which works fine in IE:
function noRightClick() {
      if (event.button==2) {
            alert('Sorry,  You can not right click.');
      }
}      
and then
document.onmousedown=noRightClick

This does not work in FireFox. What can I do to make it function similarly in FireFox?
0
Comment
Question by:Richard Korts
8 Comments
 
LVL 3

Expert Comment

by:wktang83
ID: 22732948
0
 
LVL 7

Expert Comment

by:bluV11t
ID: 22732995
Hi!
This code works in both browsers:
http://www.billybear4kids.com/clipart/riteclic.htm
<script language="JavaScript"> <!--

// No rightclick script v.2.5

// (c) 1998 barts1000

// barts1000@aol.com

// Don't delete this header!
 

var message="Sorry,  You can not right click."; // Message for the alert box
 

// Don't edit below!
 

function click(e) {

if (document.all) {

if (event.button == 2) {

alert(message);

return false;

}

}

if (document.layers) {

if (e.which == 3) {

alert(message);

return false;

}

}

}

if (document.layers) {

document.captureEvents(Event.MOUSEDOWN);

}

document.onmousedown=click;

// --> </script>

Open in new window

0
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 500 total points
ID: 22733191
This also works perfect, I got it from somewhere earlier,

<script language="javascript">
 

               var message="Right click is not allowed.";

               function clickIE4(){
 

                             if (event.button==2){

                             alert(message);

                             return false;

                             }

               }
 

               function clickNS4(e){

                             if (document.layers||document.getElementById&&!document.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>

Open in new window

0
 

Author Comment

by:Richard Korts
ID: 22734718
To bluV11t:

Does not work in FireFox. Same as before. I did not try in IE.

However, I may not be using it right.

I put it in it's own <script></script> tag set, I have a lot of other JavaScript already in there, I left that alone but REMOVED the previous document.onmousedown
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 7

Expert Comment

by:bluV11t
ID: 22735104
Did you specify language in the script tag? Tested it in both browsers...

In FF Ctrl+Shift+J to check any javascript errors. If error console is full of errors, click clear button and reload page.
0
 

Author Comment

by:Richard Korts
ID: 22736255
To bluV11t:

I combined all the Javacript into one. It works in IE 7.0. It DOES NOT work in FireFox 5.0.

Attached is the (generated) HTML with the javascript
proofs-ol1a.txt
0
 

Author Comment

by:Richard Korts
ID: 22736311
To logudotcom:

Your solution DOES NOT work in Firefox either. I have not yet tried it in IE.
0
 

Author Comment

by:Richard Korts
ID: 22736347
To logudotcom:

Sorry, I had a JavaScript error. Yours works. You get the points.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now