Click to See Complete Forum and Search --> : disable right click question


heavenly_blue
07-23-2004, 12:15 AM
I'm disabling right click on a personal page of mine. All the examples on the net give an Alert() when you right click. Is there any way to disable right click without an alert? I want either nothing to happen when you right click, or something else besides an Alert(). Here's the code I'm using:


function disable_right_click(e)
{
var browser = navigator.appName.substring ( 0, 9 );
var event_number = 0;
if (browser=="Microsoft")
event_number = event.button;
else if (browser=="Netscape")
event_number = e.which;
if ( event_number==2 || event_number==3 )
{
alert ("Alert!");
return (false);
}
return (true);
}
function check_mousekey ()
{
var mouse_key = 93;
var keycode = event.keyCode;
if ( keycode == mouse_key )
alert ( "Alert!" );
}
function trap_page_mouse_key_events ()
{
var browser = navigator.appName.substring ( 0, 9 );
document.onmousedown = disable_right_click;
if ( browser == "Microsoft" )
document.onkeydown = check_mousekey;
else if ( browser == "Netscape" )
document.captureEvents( Event.MOUSEDOWN );
}
window.onload = trap_page_mouse_key_events;


I want to replace alert ( "Alert!" ); with something else that will still keep right click disabled. Is there anything else that works? Refreshing the page would be kinda funny.

Jona
07-23-2004, 12:25 AM
Before answering the question, I think this member should be recognized for making a proper post with care and formatting. Because of its readability, I am willing to go to much more length in answering this member's question.

Heavenly_Blue, may I ask why you are interested in disabling right-click? Not only can more experienced web users get past it, in many, many ways (see Fredmv's post stickied to the top of this forum, "Wondering how to hide your source code?"), but it is frustrating for users who do not understand why they cannot right-click on the web page. Assuming your reasons are valid -- or at least arguable -- you can remove the alert code, and when a user right-clicks, nothing should happen, since what decides whether or not a user can right click is the return value (true or false; true means they can right-click, false means they cannot).

heavenly_blue
07-23-2004, 12:34 AM
at first i thought you said i DIDN'T make a proper post and it's readability was off - but then i read what you wrote again and understood. haha i'm soooo tired right now.

this is for a personal site of mine, and only i will be accessing it along with maybe some friends. the purpose is just to learn about the different things you can do with javascript.

i would never use this on a site where i would want users to stay and appreciate the content. thanks for the quick and informative reply.

Jona
07-23-2004, 12:41 AM
You're welcome. Has your problem been solved now?

heavenly_blue
07-23-2004, 12:56 AM
actually removing the alert(); lines makes right clicking possible again. it's the alert box that pops up that kills the right click menu. i wonder if there any other possible methods. i tried:


window.location.reload();


in place of the alerts as well but it doesnt work...

Jona
07-23-2004, 12:58 AM
That is because you are using Internet Explorer. Try replacing this part of the code.


function check_mousekey ()
{
var mouse_key = 93;
var keycode = event.keyCode;
if ( keycode == mouse_key )
return false;
}