Solved

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

Posted on 2011-02-21
25
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

726 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