首页技术为王JavascriptJS入门到精通 第18章 Javascript的安全

JS入门到精通 第18章 Javascript的安全

分类Javascript时间2026-07-08 16:59:58发布信义之树浏览104
导读:一、练习 1. 练习1 (1)源代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>屏蔽部分按键</title>...

一、练习

1. 练习1

  (1)源代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
<title>屏蔽部分按键</title>
<script type="text/javascript">
function keydown(){
    if(event.keyCode==8){
        event.returnValue=false;
        alert("当前设置不允许使用Backspace键");
    }
    if(event.keyCode==13){
        event.returnValue=false;
        alert("当前设置不允许使用Enter键");
    }
    if(event.keyCode==116){
        event.returnValue=false;
        alert("当前设置不允许使用F5键");
    }
    if((event.altKey)&&((window.event.keyCode==37)||(window.event.keyCode==39))){
        event.returnValue=false;
        alert("当前设置不允许使用Alt+方向键←或方向键→");
    }
    if((event.shiftKey)&&(event.keyCode==121)){
       event.returnValue=false;
       alert("当前设置不允许使用Shift+F10快捷键");
    }
}
</script>
</head>

<body onkeydown="keydown()">
    
<div style="width: 755px;margin: 0 auto">
  <img src="emilp.jpg">
</div>
    
</body>
</html>

   

 (2)运行页面

    https://www.xinyizhishu.top/jsC/sc/18_1/

 (3)Tips

   event.returnValue=false;

   event.shiftKey,event.keyCode。

   onkeydown="",这个支持。


2. 练习2

  (1)源代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
<title>屏蔽鼠标右键</title>
<script type="text/javascript">
    
  function click() {
      
     event.returnValue=false;
      
     alert("当前设置不允许使用鼠标右键!");
  }
    
  document.oncontextmenu=click;
    
</script>
</head>

<body>
    
<div style="width: 755px;margin: 0 auto">
  <img src="emilp.jpg">
</div>
    
</body>

</html>

   

 (2)运行页面

    https://www.xinyizhishu.top/jsC/sc/18_2/

 (3)Tips

   domucent.oncontextmenu=click;

   event.returnValue=false;


3. 练习3

  (1)源代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
<title>禁止复制网页内容</title>
</head>
<body onselectstart="return false">
    
<div style="width: 782px;height: 606px;margin:0 auto;background:url('bg.jpg')">
    
    <div style="width: 337px;text-indent:5px;padding-top: 331px;margin-left:257px; font-size:12px;">1
      、免费提交加盟申请;
      <p>2 、加盟申请通过公司审核后,加盟商会收到“ 金宝宝特许经营合同
        ”,并与公司签订合同 ;</p>
      <p>3 、加盟商缴纳保证金;</p>
      <p>4 、加盟商汇首批进货款,公司收到首批进货款后发货;</p>
      <p>5 、加盟商收货验货;</p>
      <p>6 、加盟店正式营业。</p>
    </div>
</div>
    
</body>
</html>

   

 (2)运行页面

    https://www.xinyizhishu.top/jsC/sc/18_3/

 (3)Tips

   onselectstart="return false"

   很弱,能改,不够高级。


二、编程训练

1. 训练1

 (1)源代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
<title></title>
<script type="text/javascript">
function keydown(){
    
    if((event.ctrlKey)&&(event.keyCode==67)){
        
       event.returnValue=false;
       alert("当前设置不允许使用Ctrl+C进行复制");
        
    }
}
</script>
</head>

<body onkeydown="keydown()">
    
<div>
  欢迎来到明日学院官方网站
</div>
    
</body>
</html>

  (2)运行页面

    https://www.xinyizhishu.top/jsC/sc/18_4/

  (3)Tips

    event.ctrlKey   event.keyCode==67

    还是很弱。


2. 训练2

 (1)源代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
<title></title>
<script type="text/javascript">
    
  window.addEventListener('DOMContentLoaded',function(){
      
    document.getElementById("demo").oncontextmenu=function(){
      event.returnValue=false;
      alert("不允许使用鼠标右键!");
    }
    
   });
</script>
</head>

<body>
    
<p id="demo">
  JavaScript从入门到精通
</p>
    
</body>

</html>

  (2)运行页面

    https://www.xinyizhishu.top/jsC/sc/18_5/

  (3)Tips

    .oncontextmenu=funtion(){}


三、实践与练习

1. 实践1

 (1)源代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
<title></title>
<script type="text/javascript">
    

    
 window.addEventListener('DOMContentLoaded',function(){
     
    document.onselectstart=function (){ 
      alert("不允许选择文本!");
      return false;
    }    
    
  });
    
  

</script>
</head>

<body>
    
<p id="demo">
  欢迎来到明日学院官方网站
</p>
    
</body>
</html>

  (2)运行页面

    https://www.xinyizhishu.top/jsC/sc/18_6/

  (3)Tips

    document.onselectstart=function(){}

    在edge里鼠标点一下就弹。


2. 实践2

 (1)源代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
<title></title>
<script type="text/javascript">
    
 window.addEventListener('DOMContentLoaded',function(){
     
    document.oncontextmenu=function(){
      if(!event.ctrlKey){
        event.returnValue=false;
        alert("不允许使用鼠标右键!");
      }
    }
    
});
    
</script>
</head>

<body>
    
<p id="demo">
  JavaScript从入门到精通
</p>
    
</body>

</html>

  (2)运行页面

    https://www.xinyizhishu.top/jsC/sc/18_7/

  (3)Tips

    document.oncontextmenu

    !event.ctrlKey




















JS入门到精通 第17章 Javascript中的XML JS入门到精通 第21章 仿豆瓣电影网

游客 回复需填写必要信息