Skip to content Skip to sidebar Skip to footer

How To Disable Combination Of Keys Using Javascript?

Advance Thanks for the replies. I would like to disable view source shortcut key for IE using javascript. To disable 'Ctrl + C', I am using the followinf function: function disable

Solution 1:

Every browser has its built-in functionality to view the source code or the webpage. We can do one thing. That is disabling right click in your page.

For disable right click use the following code:

<SCRIPT TYPE="text/javascript">
function disableselect(e){
return false
}
function reEnable(){
return true
}
//if IE4+
document.onselectstart=new Function ("return false")
//if NS6
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
</SCRIPT>

Remember one thing. We can view this source using firebug or some other third party tools. So we cant do this 100%.


Solution 2:

You should not really prevent viewing code. Why?

Reasons

  1. Eventually after all your efforts, if someone is determined he can still see your code.

  2. You will defame your website by doing this.

  3. You will kind-of behave like a "noob", as normally fellow developers see codes and they will break through your security measure by just disabling javascript.

  4. There is no sensitive information in your code(I suppose) which can be used to pose a threat. But, if you have some code which can be used against the website you should really look into removing that code and making the website secure.

Disable combinations

  document.onkeydown = function(e) {
        if (e.altKey && (e.keyCode === 67||e.keyCode === 86)) {//Alt+c, Alt+v will also be disabled sadly.
            alert('not allowed');
        }
        return false;
};​

Any ways, because I know how to do it, I will show you.

Here to disable right-click:

function clickIE() {if (document.all) {return false;}} 
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) { 
if (e.which==2||e.which==3) {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") 

Solution 3:

alt + V + C is a very odd combination to have. Although following code will work, it's sort of hackish.

if (event.altKey) {
    if (event.keyCode == 67 && window.prevKey == 86)
        event.preventDefault();
    else if (event.keyCode == 86 && window.prevKey == 67)
        event.preventDefault();
    window.prevKey = event.keyCode
}

Post a Comment for "How To Disable Combination Of Keys Using Javascript?"