Solved

code to disable right click and view source

Posted on 2000-04-19
4
15,976 Views
Last Modified: 2013-12-25
Previously submitted in Java but was told I should try a different category. Someone please advise if I have selected the wrong category. Thanks!

Could someone please forward me the code to disable view source and to also disable right click? I need this to work cross-browser 3.0 and up. Thanks.

(example exists at http://www.dotcomguy.com then click to watch him live. If you right click on the screen, a java window pops up thanking you for your support then you have to click ok to resume view. I believe the view source is disabled just by doing a javascript launched page and disabling tool bars but the right click thing has me stumped)
0
Comment
Question by:mediagirl
4 Comments
 
LVL 18

Expert Comment

by:mgfranz
Comment Utility
You can disable the right click in a page, but not the "View Source" button, so usnless yo are opening up a new window with buttons disabled this won't do much good.  But here is the code anyway;

<html>
<head><SCRIPT LANGUAGE="Javascript">
<!-- Hide from old browsers
function doNotSelect()
{
  return false;
}
document.onselectstart = doNotSelect;
// End Hide -->
</script>
</head>
<BODY OnLoad="doNotSelect()" onClick="doNotSelect()" onRightClick="doNotSelect()">
Test
</body>
</html>
0
 
LVL 2

Accepted Solution

by:
dog42 earned 300 total points
Comment Utility
Place this in your head tag:

<script language="javaScript">
function right(evnt){
 if (navigator.appName.toUpperCase().match(/NETSCAPE/) != null) {
  if (evnt.which >= 2){
   alert("We appreciate your support!");
   return false;
  }
 }else if (event.button>=2) alert("We appreciate your support.");
}

document.onmousedown = right;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
window.onmousedown=right;
</script>

I got this from dotcomguy by using the contextmenu button on win95 keyboard.  To stop people doing this, add this to your body tag:

<body onContextMenu="alert('message');return false;">

Hope this helps,
Dog42

0
 
LVL 1

Author Comment

by:mediagirl
Comment Utility
Thanks bud. I appreciate your preciseness and speed of an accurate response!
0
 

Expert Comment

by:kcoupland
Comment Utility
Why would you want to disable right-click? Just think about how often the right-click (or left-click - lefties) is used.

If the code (JavaScript mostly) or images are to be protected then put in some copyright notices. Disabling right click I think is unprofessional and a sign you don't trust your visitors or clients.

If you want to protect something that badly, why have it on the web?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
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.

743 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

13 Experts available now in Live!

Get 1:1 Help Now