如何使用javascript禁用键组合

How to disable combination of keys using javascript?

本文关键字:组合 何使用 javascript      更新时间:2023-09-26

我想使用JavaScript禁用IE的视图源快捷键。要禁用Ctrl+C,我使用以下功能:

function disableCopy() {
     // current pressed key
     var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
     if (event.ctrlKey && (pressedKey == "c")) {
         // disable key press porcessing
         event.returnValue = false;
     }
}

有人能建议如何禁用Alt+V+C

每个浏览器都有查看源代码或网页的内置功能。我们可以做一件事。这将禁用您页面中的右键单击。

要禁用右键单击,请使用以下代码:

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

记住一件事。我们可以使用firebug或其他一些第三方工具查看此源代码。所以我们不能100%做到这一点

您不应该真正阻止查看代码。为什么?

原因

  1. 最终,经过你所有的努力,如果有人下定决心,他仍然可以看到你的代码。

  2. 你这样做会诽谤你的网站。

  3. 你的行为有点像"noob",因为通常其他开发人员看到的代码,他们会通过禁用javascript来突破你的安全措施。

  4. 你的代码中没有可以用来构成威胁的敏感信息(我想)。但是,如果你有一些代码可以用来对付网站,你真的应该考虑删除这些代码,让网站安全。

禁用组合

  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;
};​

不管怎样,因为我知道怎么做,我会给你看的。

此处禁用右键单击:

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") 

alt+V+C是一个非常奇怪的组合。尽管下面的代码可以工作,但它有点像黑客。

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
}