How to prevent copying of website content
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.
Recommended: "js tutorial"
Sometimes the requirement is that there are some contents in the website that you don’t 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
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
:<body oncopy="alert('对不起,禁止复制!');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('对不起,禁止复制!');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('不能Ctrl+C和Ctrl+V复制、粘贴'); return false; } }); </script> </body> </html>
If there are any errors, please contact me to correct them, thank you very much! ! !
For more programming-related content, please pay attention to the Programming Introduction column on the php Chinese website!
The above is the detailed content of How to prevent copying of website content. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.
