Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

No Right CLick

Status
Not open for further replies.

chasdrury

IS-IT--Management
Aug 8, 2001
12
0
0
GB
Hi

What is the code for no right click on a HTML page?

Cheers

Chaz
 
There are two ways I know of, one which I am not so eager to teach everyone! The other looks somehthing like this (keep in mind that I'm writing from my memory, if any problems just post a message here and I correct any errors).
And I'm not sure if this works in Netscape, but I think it should!!

Code:
<script language=javascript>
document.onmousedown = noway;
function noway()
{
if( event.button==2||event.button==3)
  {
   alert(&quot;Eeeh... Don't use that mouse button&quot;);
  }
}
</script>

Remember that most JavaScript can be used for various things... for example, you can use this code (which triggers on the entire HTML document) to avoid the user from rightclicking on an image! (as I said before, there is one more way that I know of that works only in explorer (as far as I know) which is more secure than this one) there is an easy way around this =) If you found out what... then you can use it on most pages with this kind of code!! My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
I tested your code and got the error saying

event is not defined.

on this line if(event.button==2 || event.button==3)

Regards
 
As I said, I was not sure if it would work in other browsers then Explorer (haven't been fooling around with JS for a very long time) I'll come back though as soons as I remember how to do it in netscape!! My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
I use this code on my website, works in explorer and netscape

<script language=JavaScript>
var message=&quot;Wrong button&quot;;
function click(e){
if (document.all){
if (event.button==2||event.button==3){
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>
 
Why not do a keyword search on this forum for &quot;no right click&quot;? This question comes up here at least once every 2 weeks and I'm sure there's a plethora of different methods posted which suit either browser.

Greg.
 
This works in netscape and explorer. Have fun with it

<script language=JavaScript>
var message=&quot;Wrong button&quot;;
function click(e){
if (document.all){
if (event.button==2||event.button==3){
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>
 
Remember that when an alert box is shown, the page is more vulnerable then without... and sometimes the return false; may not be adequate. I can still copy images with the right click function... it is NOT 100% fool proof! Even a fool could learn how to bypass it!

When using the event.button for explorer, you can easily bypass it... it wasn't that hard to figure out, and I'm not talking about disabling JavaScript or anything like that! My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
why do you want to disable right clisk i have never heard a decent reason. I dont want your images and if i did i could get them from the cache....

all your doing is preventing me using the 'open in new window' feature which i like and use a lot....
 
all your doing is preventing me using the 'open in new window' feature which i like and use a lot....

No he isn't ... it's faster to hold down Shift and left-click on the link anyway.

Greg.
 
Crazyboybert, the reasons are many (yeah, right)... it's true that you can get the images from cache, but not everbody knows about that! Most of the ppl surfing the net doesnt care to learn how it works... another thing is to make it harder for the surfer to copy ones images!

How can it prevent you from using the &quot;open in new window&quot; feature?! if you are trying to use the right click to open a new window, then just give the link a name / id or anything like that, then just rewrite the function to something like this: (don't know how to do it in netscape)
Code:
document.onmousedown=noc;
function noc()
{
 if(event.srcElement.ID == &quot;AllowRC&quot; )
  {
   //code for allowing right click feature
  }
 else
  {
   //code for removing right click feature
  }
}

not sure if I wrote it correctly though... it only works in IE (>5) i believe, never tried it in an earlier version! My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Aug 9, 2001
This works in netscape and explorer. Have fun with it

<script language=JavaScript>
var message=&quot;Wrong button&quot;;
function click(e){
if (document.all){
if (event.button==2||event.button==3){
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>

Easy to get around the dual disabled buttons, hold in the left and move the mouse slightly and you get a circle with a line in it, right click...thier you have it, grab the text or pic.

Only FLASH will hide, the rest are a waste of time
 
Sure, it isn't bullet-proof, anyway, everybody can get in source plus you can find pictures in cache.

But keeps away the not so computer-minded people away for copying, those who know can get it anyway.

Only thing you can do....don't put it on the web.
 
What the frell... this how do DISABLE the rightclick function (only works in IE)
the trick is NOT to call the onmouse event, but to to call it's ancestor contextmenu there is NO way around this one
There are ways that which I use to improve security on my pages, using simple and plain JavaScript! I would like to see the man or woman who can steal the code on my page... =)

Anyhow, here is the more secure way in IE
Code:
document.oncontextmenu = nope;
function nope()
{
alert(&quot;This is the best way, in explorer&quot;);
return false; // there is no use for the aler
}

Note that I say DISABLE the rightclick, not preventing the user from copying your images/text via drag methode! The user is just unable to get the contextmenu visible... there is a way around the image drag-and-drop function and that is to use layers, try this simple but efficent code, you will notice that you still CAN drag the image, but not drop it!! Neither to a new explorer window, nor to anywhere! Try to figure out a way around this one
Code:
<div id=&quot;test&quot; style=&quot;background-image:url([image-url]);background-color: #0000FF;width:[image width]px; height:[image height]px;&quot;></div>
<script language=javascript>
document.oncontextmenu = Nope;
function Nope()
{
 alert(&quot;This is the best way, in explorer&quot;);
 return false;
}
</script>
My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top