Tuesday, September 25, 2012

Disable Right Click In a Web Page

We can disable right click on apage using the following code

  <script type="text/javascript">
        document.onmousedown = clickfn;

        function clickfn(e) {
            var button;
            if (navigator.appName == "Microsoft Internet Explorer") {
                button = event.button;
              
            }
            else {
                button = e.buttons;
            }

            if (button == 2) {
                alert("Right Click Disabled");

                if (navigator.appName == "Microsoft Internet Explorer") {
                    event.returnValue = false;
                }               
                return false;
            }
        }
    </script>



navigator.appName == "Microsoft Internet Explorer"  this is used because in ie 7 "e" is not supported so we used event in common for all ie versions...

2 comments:

  1. nice share gan, sekedar tambahan untuk chrome perlu ngehandle even document.oncontextmenu juga, klo ndak gitu menu klik kanan nya masih keluar..

    ReplyDelete
    Replies
    1. bagaimana cara handle document.oncontextmenu nya..?
      boleh minta contoh codes nya..?

      Delete