zoukankan      html  css  js  c++  java
  • HTML 禁止复制文字

    有时我们会遇到禁止用户复制网站的文字内容,例如小说网站,我们可以通过下列方法提高复制内容的门槛,来实现我们的目标。

    1.禁用选中和右键:

    在<body>标签中添加以下代码:

    oncontextmenu='return false'    禁止右键
    ondragstart='return false'    禁止拖动
    onselectstart ='return false'    禁止选中
    onselect='document.selection.empty()'    禁止选中
    oncopy='document.selection.empty()'    禁止复制
    onbeforecopy='return false'    禁止复制
    onmouseup='document.selection.empty()' 

    如下:

    <body leftmargin=0 topmargin=0 oncontextmenu='return false' ondragstart='return false' onselectstart ='return false' onselect='document.selection.empty()' oncopy='document.selection.empty()' onbeforecopy='return false' onmouseup='document.selection.empty()'>
    2.禁止网页另存为:在<body>后面加入以下代码: 

    <noscript> 
    <iframe src="*.htm"></iframe> 
    </noscript>  
    这时在电脑端已经无法选择复制,但是在移动端还可以选中复制,再添加以下css代码用来禁止选中文字。

    3.禁止选中文字

    *{
    moz-user-select: -moz-none;
    -moz-user-select: none;
    -o-user-select:none;
    -khtml-user-select:none;
    -webkit-user-select:none;
    -ms-user-select:none;
    user-select:none;
    }
    这时正常的选择复制都已经被禁用但是还可以用浏览器的查看源码和调试工具来直接从代码中复制内容。

    4.禁用F12按键

    //禁用F12
    window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
    // 判断是否按下F12,F12键码为123
    if (event.keyCode == 123) {
    event.preventDefault(); // 阻止默认事件行为
    window.event.returnValue = false;
    }
    }
    5.禁用调试工具

    var threshold = 160; // 打开控制台的宽或高阈值
    // 每秒检查一次
    var check = setInterval(function() {
    if (window.outerWidth - window.innerWidth > threshold ||
    window.outerHeight - window.innerHeight > threshold) {
    // 如果打开控制台,则刷新页面
    window.location.reload();
    }
    }, 1000);
    至此,已经限制了大部分的复制功能,但是还不能彻底禁止,更完善的方法还需学习整理。

     

  • 相关阅读:
    POJ-1700 Crossing River
    C++操作符new-动态存储空间分配
    POJ-3978-Primes
    POJ-1316-Self Numbers
    浅谈二维数组在传参中的使用
    OneToMany与ManyToOne的属性
    jquery的学习
    struts2常用标签
    Struts2.xml中result type属性说明
    监听器Listener
  • 原文地址:https://www.cnblogs.com/shaozhu520/p/10704440.html
Copyright © 2011-2022 走看看