user123
user123

Reputation: 850

How to disable the clt+u for browser?

when i browse for to disable the right click and short cut o the browser, found the solution in this url

http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/138420/how-to-disable-view-source-and-ctrlc-from-a-site

here is the script working for disable the right-click but not for (control+u) shortcut. how could disable the keyboard events means (Ctrl + u event):

<script>
     var isNS = (navigator.appName == "Netscape") ? 1 : 0;
    if(navigator.appName == "Netscape")               
   document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
    function mischandler(){
     return false;
  }
      function mousehandler(e){
     var myevent = (isNS) ? e : event;
          var eventbutton = (isNS) ? myevent.which : myevent.button;
        if((eventbutton==2)||(eventbutton==3)) return false;
       }
        document.oncontextmenu = mischandler;
       document.onmousedown = mousehandler;
        document.onmouseup = mousehandler;
</script>

Upvotes: 1

Views: 133

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117354

<!--[if ! IE]><!-->
<script>
document.addEventListener('keydown',function(e){
  if(e.ctrlKey && e.keyCode===85){e.preventDefault();return false;}
});
</script>
<!--<![endif]-->

It's enclosed in a conditional comment that prevents IE from parsing the script(IE<9 doesn't support addEventlistener and AFAIK IE doesnt use CTRL + U for viewsource)

However, I wouldn't suggest to use it, you'll never know which shortcuts are bound to the browser(although it seems that the most browsers use this shortcut for viewsource, it could be that the shortcut has bee modified by the user)

Users that are interested in the source of the page will not have any problems to access the source somehow.

The only rational reason to use it IMHO would be when you have another application bound to this shortcut(e.g. an editor where you use it for text-formatting)

Upvotes: 1

Related Questions