Main Tutorials

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 Author

author image
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

Subscribe
Notify of
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
satyam
4 years ago

Why it Not working for ctrl+u…
I want no one can see my source code .. kindly tell me what to do. Send me some reference in my mail id .

ellipsis
10 years ago

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?

mkyong
7 years ago
Reply to  ellipsis

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

AtomAnt
9 years ago
Reply to  ellipsis

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

Audry
11 years ago

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
11 years ago

very helpful information thanks

Donella Ledet
12 years ago

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.