JavaScript – How to disable right click button on a website

Many webmasters like to disable the right click button to protect their own content. Here is the JavaScript to disable right click button on a website :


<script>
    <!--
    //edit this message to say what you want
    var message = "Function Disabled";

    function clickIE() {
        if (document.all) {
            alert(message);
            return false;
        }
    }
    function clickNS(e) {
        if (document.layers || (document.getElementById && !document.all)) {
            if (e.which == 2 || e.which == 3) {
                alert(message);
                return false;
            }
        }
    }
    if (document.layers) {
        document.captureEvents(Event.MOUSEDOWN);
        document.onmousedown = clickNS;
    }
    else {
        document.onmouseup = clickNS;
        document.oncontextmenu = clickIE;
    }

    document.oncontextmenu = new Function("return false")
    // -->
</script>

Website examples

  1. http://www.dynamicdrive.com/dynamicindex9/noright.htm
  2. http://www.users.totalise.co.uk/~bigoleg/javascript/javascript_disable_right_click.html

1. How to enable the right click button?

Just copy the following script and paste into the browser address barr. Right click button will be enabled again


javascript:void(document.oncontextmenu=null) 
Note
For modern browser, it will delete the prefix “javascript:” automatically and fire the default search engine, to fix it, paste above script and type the prefix javascript: manually.

2. counter Attack!

To counter the above hack, put a oncontextmenu="return false; into the HTML body tag like this :


<body oncontextmenu="return false;">  

Now, puts javascript:void(document.oncontextmenu=null) into browser address bar will not works.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
aartimkyongAtomAntellipsisAudry Recent comment authors
newest oldest most voted
ellipsis
Guest
ellipsis

Hi,

In modern browsers (like chrome, ff) when we paste ” javascript:void(document.oncontextmenu=null) “, it just activates the default searchengine of the browsers and searches the web for those words. Is there any workaround for that?
May be I am misunderstanding something here?

AtomAnt
Guest
AtomAnt

Hi, i know this is a little late, but maybe someone finds this usefull.
In modern browser when you paste ” javascript:void(document.oncontextmenu=null) “, the browser strips the text of the “javascript:” from the begining and only pastes “void(document.oncontextmenu=null)” resulting in a search rathen then a javascript command or injection or whatever. Therefore, you have to manually type “javascript:” before the “void(..etc)”
Maybe that happend to you too and you just didn’t notice.
Hope this helps

mkyong
Guest
mkyong

Type the prefix “javascript:” manually and it works on latest Chrome.

Audry
Guest
Audry

Hi, Neat post. There’s a problem together with your web site in internet explorer, could check this? IE still is the marketplace chief and a good component to folks will miss your great writing due to this problem.

MS
Guest
MS

very helpful information thanks

Donella Ledet
Guest
Donella Ledet

I have realized some new items from your internet site about computers. Another thing I have always thought is that computer systems have become something that each residence must have for some reasons. They offer convenient ways in which to organize homes, pay bills, shop, study, focus on music and perhaps watch shows. An innovative solution to complete these tasks is by using a notebook computer. These computer systems are portable ones, small, robust and lightweight.