Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 6th, 2006, 09:16 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default disable right-click on FF and OP

Code:
// Prevent page from being right-clicked at any point
// for images, view source etc....
if (window.Event)
{
    document.captureEvents(Event.MOUSEUP);
}

function nocontextmenu() 
{
    event.cancelBubble = true, event.returnValue = false;
    return false;
} 

function norightclick(e) 
{
    if (window.Event) 
    {
        if (e.which == 2 || e.which == 3) 
        {
            return false;
        }
        else if (event.button == 2 || event.button == 3) 
        {
            event.cancelBubble = true, event.returnValue = false;
            return false;
        }
    }
}

if (document.layers)
{
    document.captureEvents(Event.MOUSEDOWN);
}

document.oncontextmenu = nocontextmenu;
document.onmousedown = norightclick;
document.onmouseup = norightclick;
This code works on IE6 (and 7), but does anyone have a version that will work on FF/OP?

Picco

www.crmpicco.co.uk
www.ie7.com
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old April 6th, 2006, 02:51 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Standards-following browsers accept the event object as the first argument to the event handling function.

To make this cross-browser, you handle the event like this:

Code:
function($e)
{
   // If $e evaluates to false, assign it window.event.
   ($e = ($e || window.event));
}
If you want to cancel a right click, just do this

Code:
document.oncontextmenu = function($e)
{
    if (window.event)
    {
        window.event.returnValue = false;
    }
    else
    {
        $e.preventDefault();
    }
};
Opera does not support the "oncontextmenu" event.

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
 
Old April 12th, 2006, 03:02 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

thanks Richard, I read up on the Opera situation and it seems you can't disable right-click on Opera at all.

i wanted it just for effect, not to protect code or anything.

Cheers.

Picco

www.crmpicco.co.uk
www.ie7.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
disable browser right click?? danielnixon HTML Code Clinic 1 May 19th, 2008 12:11 PM
How to disable Right-click to Save Picture As.... bekim HTML Code Clinic 11 January 9th, 2005 02:09 AM
Disable Right-click for Save Image As... bekim Javascript How-To 5 January 7th, 2005 09:03 PM
Flash object: disable right click on it karib Flash (all versions) 4 April 25th, 2004 01:08 AM
disable right click for onMouseUp dhaywirex Classic ASP Basics 2 March 4th, 2004 05:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.