Solved

JavaScript - Disable rightclick code doesn't work with all browsers!

Posted on 2011-02-21
25
232 Views
Last Modified: 2012-05-11
Hi,

The below worked out for me in IE only, how do I manage it with other browsers?


<script language="javascript">


    var message="Sorry, Right Click is disabled.";
   
    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
Comment
Question by:feesu
  • 11
  • 10
  • 3
  • +1
25 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 167 total points
Comment Utility
0
 

Author Comment

by:feesu
Comment Utility
It doesn't work with Chrome!
0
 
LVL 11

Assisted Solution

by:govindarajan78
govindarajan78 earned 167 total points
Comment Utility
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
>It doesn't work with Chrome!

With the test page provided ? Because it work for me...
0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 166 total points
Comment Utility
add this to body tag

<body oncontextmenu="return false;">
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
0
 

Author Comment

by:feesu
Comment Utility
leakim971,
What test page? I copied the code, and it works fine in IE and Firefox.

pratima_mcs,
Adding that in the body has another problem, if you right click at the very bottom of the page it works, and you can get a context menu, maybe cuz it is restricted to only body and not the whole page! Add some content, and try, it will disable the right click on the content, but if you right click at the bottom in the white space, it accepts! In all browsers!
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
>What test page? I copied the code, and it works fine in IE and Firefox.

OK, the code in the link. I tested it on my Chrome browser, do a right click and the context did not appear.
0
 

Author Comment

by:feesu
Comment Utility

govindarajan78,

Below is the first comment posted on your article:

I don’t know if this works for Firefox, but it does NOT work on Google Chrome… the Function Disabled alert does come up but users can still see and use the right click menu to save images from the website : (
0
 

Author Comment

by:feesu
Comment Utility
leakim971,

To me it doesn't work, it works for everything except Chrome!
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
my version : 9.0.597.98
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
the try on html tag

<html oncontextmenu="return false;">
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:feesu
Comment Utility
pratima_mcs,
On html tag solved my problem, do you think that there is a workaround that users can use?
0
 

Author Comment

by:feesu
Comment Utility
leakim971,
How do I check my Chrome version?
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
tools menu -> About Chrome
0
 

Author Comment

by:feesu
Comment Utility
9.0.597.98
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
very strange... I tested it again, no context menu...
0
 

Author Comment

by:feesu
Comment Utility
I am surprised, but <html oncontextmenu="return false;"> worked fine for me, and across all browsers.

Do you think that this has a bug or a gap? Why not so many people using it?
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Test it with Opera
I don't know what you're trying to protect but if it is source or image link, people will always able to choose view source (via the browser menu) to get what they want.
0
 

Author Comment

by:feesu
Comment Utility
I care for IE,Chrome, and FF mostly.

I can open it it with popup that does not have a tool bar or a menu. Just like banks websites do, or do you have another idea?
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
I'm ok if you're agree that you only reduced the number of people able to get the content of your page only. Else, we can reload your page with a menu and so on...
0
 

Author Comment

by:feesu
Comment Utility
I give up, how do banks and online examination sites work?

I wish to have a page that does not allow users to print/copy any content!
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
>I give up, how do banks and online examination sites work?

applet, flash ? which sites ? you will always able to get content of a page. It only depend the knowledge of the user.

if you click on the print button, you're able to do screen copy for example but ok it's only a picture
0
 

Author Comment

by:feesu
Comment Utility
Ok but at least only minimum number of users will be able to do that! Majority won't!
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
yes that is !
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The viewer will learn how to dynamically set the form action using jQuery.
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)

728 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

10 Experts available now in Live!

Get 1:1 Help Now