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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling right-click problems

Status
Not open for further replies.

rpeters74

Programmer
May 24, 2002
46
US
I use the following code to disable the 2nd and 3rd mouse button clicks:

======================================

if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2))
{
return false;
}
else if (navigator.appName == 'Microsoft Internet Explorer' &&
(event.button == 2 || event.button == 3))
{
alert("Action not allowed.");
return false;
}
return true;

======================================

If the borwser is IE I needed to put that alert box in these in order to capture the clicks. (i.e. I could not just return false in this instance as the right button was still firing off). Any ideas on how I can do this in IE without having an alert box or any other type of user-required action?

-rpeters
 
This is the script that I use, and it works in both IE and NS...
Code:
<script language=JavaScript> 
  var message=&quot;Sorry, this function is disabled.&quot;; 
  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>
I have not failed; I merely found 100,000 different ways of not succeding...
 
If you're trying to keep people from seeing your code or capturing your pictures, don't bother trying. Everything is still readily available through the browser and Temporary Internet Files or other cache. I don't know HOW many times we've been over this one on this forum.
 
Its one of those things that just keeps comming back and again and again...lol... I have not failed; I merely found 100,000 different ways of not succeding...
 
If you are asking how to disable right-click for IE without having alerts pop up the simpliest solution is to add this code to your body:

oncontextmenu=&quot;return false&quot;

like this:

<body oncontextmenu=&quot;return false&quot;>

Of course I feel I should ask you why you even bother with this no-right-click crap in the first place since there are soooo many ways to trick it... Let me just mention three of them that work with some or all no-right-click scripts just to let you understand how lame they are...

1. open opera and right click like you would in ie!
2. view source and get anything you want from within ie!
3. click and hold left button then click right button and you'll have the right click menu anyway...

many more tricks available... ;-)
Good luck!
 
Why jump on someone for trying to do something. There are other reasons for disabling right click, other than hiding code permanently.

I have page I wrote for my son to help him with his math, I disabled right click because the answers are in the code of the page, It's not that I never want anyone to see the code, it just I don't want him to get the answers from the code (when he's trying to answer the question) and he's timed on the page (auto submit when time runs out) so going to cache gets rulled out, and the page is in pop-up with no browser menus, you could use the no-cache feature to disable the caching of the page.

Most users would never think of going to cache (let alone now where to find the folder) or the click and hold left button then click right button and you'll have the right click menu.

if it's a pop-up use window.showmodaldialog().
 
Realistically, the vast majority of people who ask this question are newbies who want to try to protect something they did they believe is unique or special. The bottom line is that the Internet is NOT the place to display things you want to protect, the whole thing is about shared information. And anyone with half a brain and a little experience who's out looking for pictures or code to take off websites knows how to do it. You're right that &quot;most users would never thinking of going to the cache&quot;, but only because most users aren't interested in viewing the source or grabbing graphics off web pages.

I wrote a math web page for an elementary school teacher (3rd grade) to use, but had it do the calculations based on the math problem it developed at random, and it just checked the answer stored in a variable against what the student gave. That would provide a lot more possibilities for math problems, and no possibility of peeking at the code to see the answer.
 
Mine does create them on the fly, but the page displays a check mark if answered correctly and and x if its wrong he can also click a help buttong to get &quot;If I have three apples and you give me 5 apples. How many apple's do I have&quot;. In order to do what I have done the java funtions need to know the values of the equation and the answer to check it. So I have onblur=&quot;CheckAnswer(1,8,3,'+')&quot; 1 is the question number so it nows where to put the check mark 8 is the first number and 3 is the second the operator is provided so 1 function can check any equation, add, sub. etc
 
Just to share ideas, here's a copy of the math problem page I wrote. I didn't comment it very well, but it should be fairly easy to vary the range of numbers used.

<html>
<head>
<title>Math Work</title>

<script language=&quot;javascript&quot;>

// this part gets the previous score, if there was one

var pagename=location.href, results, right=0, wrong=0, notdone=0;
if (location.href.indexOf('?') > -1)
{
pagename=location.href.substr(0,location.href.indexOf('?'));
results=location.href.substr(location.href.indexOf('?')+1);
right=results.substring(results.indexOf('right=') + 6, results.indexOf('&'));
results=results.substr(results.indexOf('&') + 1);
wrong=results.substring(results.indexOf('wrong=') + 6, results.indexOf('&'));
results=results.substr(results.indexOf('&') + 1);
notdone=results.substring(results.indexOf('notdone=') + 8);
}

var total=(right * 1) + (wrong * 1) + (notdone * 1);

var NumberOfProblems=10;
var MaximumNumber=10000;

//this just randomly picks either subtraction, addition, or multiplication
var sign=Math.floor(Math.random() * 12);

switch (sign % 4)
{
case 0: sign='+'; break;
case 1: sign='-'; break;
case 2:
case 3:
{
sign='*';
MaximumNumber=12;
break;
}
}
MaximumNumber++;
var answercount=0;
var number1=Math.floor(Math.random() * MaximumNumber);
var number2=Math.floor(Math.random() * MaximumNumber);

//this makes sure the answer isn't a negative number
if (sign=='-' || sign=='*')
{
while (number2 > number1) number2=Math.floor(Math.random() * MaximumNumber);
}

function checkanswer()
{
var correct= eval('document.math.result.value==(number1' + sign + 'number2)');
if (document.math.result.value=='') correct=false;
if (correct)
{
alert('Very good');
right++;
nextpage();
}

else
{
answercount++;
//if they get the answer wrong more than twice, try another problem
if (answercount>2)
{
alert('Let\'s try another problem');
wrong++;
nextpage();
}
else
{
alert('Sorry, try again');
document.math.result.value='';
document.math.result.focus();
}
}
}

function nextpage()
{
location.href=pagename + '?right=' + right + '&wrong=' + wrong + '&notdone=' + notdone;
}
</script>

</head>
<body onload=&quot;document.math.result.focus()&quot;>
<center><font size=6><b>Math Work</b></font><br><br>

<form name=math onsubmit=&quot;checkanswer(); return false;&quot;>
<table cellspacing=0 cellpadding=0 border=0>
<tr>
<td align=center><table><tr><td align=right><font size=5>

<script language=&quot;javascript&quot;>

//if there have been less than the maximum number of problems, do another problem
if (total < NumberOfProblems)
{
var mathstring=number1 + '<br>';
if (sign=='*') mathstring += 'x';
else mathstring += sign;
mathstring += ' ' + number2 + '<hr width=';
if (sign=='*') mathstring += '45';
else mathstring += '85';
mathstring += '><input type=text name=result size=';
if (sign=='*') mathstring += '3';
else mathstring += '6';
mathstring += ' value=&quot;&quot;>';
document.write(mathstring);
}
//otherwise give the score of right answers, wrong answers, and problems skipped
else
{
document.write('You had ' + right + ' correct answer');
if (right!=1) document.write('s');
document.write('<br>You had ' + wrong + ' incorrect answer');
if (wrong!=1) document.write('s');
document.write('<br>You skipped ' + notdone + ' problem');
if (notdone!=1) document.write('s');
}

document.write('</td></tr></table></td></tr><tr><td align=center><br>')

if (total < NumberOfProblems)
{
document.write('<input type=button name=calculate ');
document.write('value=&quot; Calculate &quot; ');
document.write('onclick=&quot;checkanswer(); return true;&quot;><br><br>');
document.write('<input type=button name=next value=&quot; Next Problem &quot; ');
document.write('onclick=&quot;notdone++; nextpage(); return true;&quot;>');
}
else
{
document.write('<input type=button name=result ');
document.write('value=&quot; Try Again &quot; ');
document.write('onclick=&quot;location.href=pagename; return true;&quot;>');
}

</script>
</td>
</tr>
</table>
</form>

</body>
</html>
 
<sigh>

I have been developing for the Internet before it was considered as such. I have been developing traditionally for over twice that long (decades people). As Spazman pointed out the reasons for disabling the right-mouse button are my own. I did not offer a reason why I wanted to do this nor does anyone know of what other measures I have taken. Trolls do as trolls do. It seems that the people who would rather pontificate on the evils of browser manipulation have too much idle times on their hands and/or *that* is the extent of their knowledge. The argument is old and stale and I am *WELL* aware of the points that have been re-iterated in this thread ad nauseum.

</sigh>
 
I have to agree w/ Rpeters. You guys must have a lot of time on your hands. Y not just answer the question and be done w/ it. Or, if you feel so strongly, don't answer and ignore it. I would love to hear some of the problems you guys have. Oh wait, you guys don't have any. Which would allow you soooo much time to give such petty(probably not to useful either) answers.
 
Just as a final point somehow, I have been accused of NOT answering a solution to an issue. Please re-read my post.

While I did jump on few poeple who were pointing why it won't work instead of helping ...Never mind.

I did give an answer, I asked if it was a pop-up window and suggested window.showmodaldialog().

melvin we all have problems/issues, that is why we are members of this forum, now get down before I burn that soap box.

This link should help rpeters74, with the window.showmodaldialog().

If it's not a pop-up, I use this in the body tag;

oncontextmenu=&quot;alert('No Right Clicking!');return false&quot;
 
Just to clarify, I was not accusing Spazman of not answering my question. On the contrary, I was referring to his first statement in the orginal post, &quot;Why jump on someone for trying to do something.&quot; which I was agreeing with.
 
Spazman: thnx for the link and the tip ... window.showmodaldialog() is doing for me exactly what I wanted.
 
rpeters74 my last post was not directed at you, it was directed at melvin918, who did not read the thread.

I am glad I was able to help.

Anytime you need it,
SpazMan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top