Home  >  Article  >  Web Front-end  >  Several ways to prevent copying of website content using JavaScript

Several ways to prevent copying of website content using JavaScript

青灯夜游
青灯夜游forward
2021-01-26 18:55:072871browse

Several ways to prevent copying of website content using JavaScript

If you don’t want others to copy your website content, you can add this js code to your web page to block the right-click menu, copy-paste, selection, etc.

Sometimes the requirement is that there are some contents in the website that you do not want others to copy, so you need to use code control.

There are many methods:

The first method:

//屏蔽右键菜单 
document.oncontextmenu = function(event) { 

    if (window.event) { 

        event = window.event; 

    } 


    try { 

        var the = event.srcElement; 

        if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { 

            return false; 

        } 

        return true; 


    } catch (e) { 

        return false; 

    } 

} 




//屏蔽粘贴 
document.onpaste = function(event) { 
    if (window.event) { 

        event = window.event; 

    } 

    try { 

        var the = event.srcElement; 

        if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { 

            return false; 

        } 

        return true; 
        
    } catch (e) { 

        return false; 

    } 

} 



//屏蔽复制 

document.oncopy = function(event) { 

    if (window.event) { 

        event = window.event; 

    } 

    try { 

        var the = event.srcElement; 

        if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { 

            return false; 

        } 

        return true; 

    } catch (e) { 

        return false; 

    } 

} 

  
//屏蔽剪切 

document.oncut = function(event) { 

    if (window.event) { 
    
        event = window.event; 

    } 

    try { 

        var the = event.srcElement; 

        if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { 

            return false; 

        } 

        return true; 

    } catch (e) { 

        return false; 

    } 

} 


//屏蔽选中 

document.onselectstart = function(event) { 

    if (window.event) { 

        event = window.event; 

    } 

    try { 

        var the = event.srcElement; 

        if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) { 

            return false; 

        } 

        return true; 

    } catch (e) { 

        return false; 

    } 

}

The second method :

Add the following js code

<script type="text/javascript">

// oncontextmenu 事件在元素中用户右击鼠标时触发并打开上下文菜单

document.oncontextmenu=new Function("event.returnValue=false"); 

// onselectstart几乎可以用于所有对象,其触发时间为目标对象被开始选中时(即选中动作刚开始,尚未实质性被选中)

document.onselectstart=new Function("event.returnValue=false"); 

</script>
例子:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<style>

*{margin: 0;padding: 0;}

.container h1 {color: gold;text-align:center;margin-bottom:30px;}

.container p {width: 500px;margin:0 auto;color: purple;text-indent: 30px;}

</style>

</head>

<body>

<div>

<h1>火影忍者</h1>

<p>

十多年前一只拥有巨大威力的妖兽“九尾妖狐”袭击了木叶忍者村,当时的第四代火影拼尽全力,
以自己的生命为代价将“九尾妖狐”封印在了刚出生的鸣人身上。木叶村终于恢复了平静,
但村民们却把鸣人当成像“九尾妖狐”那样的怪物看待,所有人都疏远他。 
鸣人自小就孤苦无依,一晃十多年过去了,少年鸣人考入了木叶村的忍者学校,结识了好朋友佐助和小樱。
佐助是宇智波家族的传人之一,当他还是小孩的时候他的哥哥——一个已经拥有高超忍术的忍者将他们家族的人都杀死了,
然后投靠了一直想将木叶村毁灭的大蛇丸,佐助自小就发誓要超越哥哥,为家族报仇。
鸣人他们在忍者学校得到了教官卡卡西的精心指点,在他的帮助下去迎接成长中的一次又一次挑战! 

</p>

</div>

<!-- 第二种方法:通过js代码实现 -->

<script type="text/javascript">

// oncontextmenu 事件在元素中用户右击鼠标时触发并打开上下文菜单

document.oncontextmenu=new Function("event.returnValue=false"); 

// onselectstart几乎可以用于所有对象,其触发时间为目标对象被开始选中时(即选中动作刚开始,尚未实质性被选中)

document.onselectstart=new Function("event.returnValue=false"); 

</script>

</body>

</html>

The third method:

In 6c04bd5ca3fcae76e30b72ad730ca86d Add the following code:

 <body oncontextmenu="return false" onselectstart="return false"> 

或 

<body oncontextmenu="event.returnValue=false" onselectstart="event.returnValue=false">

body中加入代码的这种方法有个缺陷就是取决于body的内容,如果body内容较少,从body下方往上选中内容,
仍然是可以复制网站的内容的。

Fourth method:

If you only limit copying, you can add the following code in 6c04bd5ca3fcae76e30b72ad730ca86d:

<body oncopy="alert(&#39;对不起,禁止复制!&#39;);return false;"> 
例子:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<style>

*{margin: 0;padding: 0;}

.container h1 {color: gold;text-align:center;margin-bottom:30px;}

.container p {width: 500px;margin:0 auto;color: purple;text-indent: 30px;}

</style>

</head>

<body oncopy="alert(&#39;对不起,禁止复制!&#39;);return false;">

<div>

<h1>火影忍者</h1>

<p>

十多年前一只拥有巨大威力的妖兽“九尾妖狐”袭击了木叶忍者村,当时的第四代火影拼尽全力,
以自己的生命为代价将“九尾妖狐”封印在了刚出生的鸣人身上。木叶村终于恢复了平静,
但村民们却把鸣人当成像“九尾妖狐”那样的怪物看待,所有人都疏远他。 
鸣人自小就孤苦无依,一晃十多年过去了,少年鸣人考入了木叶村的忍者学校,结识了好朋友佐助和小樱。
佐助是宇智波家族的传人之一,当他还是小孩的时候他的哥哥——一个已经拥有高超忍术的忍者将他们家族的人都杀死了,
然后投靠了一直想将木叶村毁灭的大蛇丸,佐助自小就发誓要超越哥哥,为家族报仇。
鸣人他们在忍者学校得到了教官卡卡西的精心指点,在他的帮助下去迎接成长中的一次又一次挑战! 

</p>

</div>

</body>

</html>

The fifth method:

Disable Ctrl C and Ctrl V, code:

// 禁用Ctrl+C和Ctrl+V(所有浏览器均支持)

$(document).keydown(function(e) {

  if(e.ctrlKey && (e.keyCode == 86 || e.keyCode == 67)) {

    return false;

  }

});
例子

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<style>

*{margin: 0;padding: 0;}

.container h1 {color: gold;text-align:center;margin-bottom:30px;}

.container p {width: 500px;margin:0 auto;color: purple;text-indent: 30px;}

</style>

</head>

<body>

<div>

<h1>火影忍者</h1>

<p>

十多年前一只拥有巨大威力的妖兽“九尾妖狐”袭击了木叶忍者村,当时的第四代火影拼尽全力,
以自己的生命为代价将“九尾妖狐”封印在了刚出生的鸣人身上。木叶村终于恢复了平静,
但村民们却把鸣人当成像“九尾妖狐”那样的怪物看待,所有人都疏远他。 鸣人自小就孤苦无依,
一晃十多年过去了,少年鸣人考入了木叶村的忍者学校,结识了好朋友佐助和小樱。
佐助是宇智波家族的传人之一,当他还是小孩的时候他的哥哥——一个已经拥有高超忍术的忍者将他们家族的人都杀死了,
然后投靠了一直想将木叶村毁灭的大蛇丸,佐助自小就发誓要超越哥哥,为家族报仇。
鸣人他们在忍者学校得到了教官卡卡西的精心指点,在他的帮助下去迎接成长中的一次又一次挑战! 

</p>

</div>

 

<script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>

<script>

$(document).keydown(function(e) {

 if(e.ctrlKey && (e.keyCode == 86 || e.keyCode == 67)) {

 alert(&#39;不能Ctrl+C和Ctrl+V复制、粘贴&#39;);

   return false;

 }

});

</script>

</body>

</html>

If there are any errors, please contact me to correct them, thank you very much! ! !

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of Several ways to prevent copying of website content using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete