Solved

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

Posted on 2011-02-21
25
234 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
ID: 34942236
0
 

Author Comment

by:feesu
ID: 34942270
It doesn't work with Chrome!
0
 
LVL 11

Assisted Solution

by:govindarajan78
govindarajan78 earned 167 total points
ID: 34942286
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 82

Expert Comment

by:leakim971
ID: 34942302
>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
ID: 34942304
add this to body tag

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

Expert Comment

by:Pratima Pharande
ID: 34942310
0
 

Author Comment

by:feesu
ID: 34942333
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
ID: 34942338
>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
ID: 34942341

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
ID: 34942346
leakim971,

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

Expert Comment

by:leakim971
ID: 34942351
my version : 9.0.597.98
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 34942352
the try on html tag

<html oncontextmenu="return false;">
0
 

Author Comment

by:feesu
ID: 34942361
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
ID: 34942367
leakim971,
How do I check my Chrome version?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34942369
tools menu -> About Chrome
0
 

Author Comment

by:feesu
ID: 34942373
9.0.597.98
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34942378
very strange... I tested it again, no context menu...
0
 

Author Comment

by:feesu
ID: 34942387
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
ID: 34942400
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
ID: 34942427
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
ID: 34942445
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
ID: 34942456
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
ID: 34942476
>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
ID: 34942543
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
ID: 34942557
yes that is !
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

829 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