At present, there are fewer and fewer original webmasters who copy content everywhere. Is there any way to prohibit users from easily copying website content, next, I will introduce in detail various JavaScript programs to prevent web pages from being copied. If you need to use them, refer to this tutorial.
1. Disable right-click and copy
Method 1:
It is easy to prohibit right-click and select text code. You only need to add the following two lines of code to the javascript code of the head tag.
Add the following code to the webpage:
The Code is as follows: |
Copy code |
<Script language = "Javascript"> Document. oncontextmenu = new Function ("event. returnValue = false "); Document. onselectstart = new Function ("event. returnValue = false "); </Script> The following code can be written on a page using jQuery: Document. oncontextmenu = function (e) {return false ;} $ ('Body'). bind ("selectstart", function () {return false ;}); |
Method 2:
Add the following code to <body>:
The Code is as follows: |
Copy code |
<Body oncontextmenu = "return false" onselectstart = "return false"> Or <Body oncontextmenu = "event. returnValue = false" onselectstart = "event. returnValue = false"> |
In essence, method 2 is the same as method 1.
Method 3:
If you only restrict replication, you can add the following code in <body>:
The Code is as follows: |
Copy code |
<Body oncopy = "alert ('Sorry, copying is prohibited! '); Return false; "> |
2. invalidate the menu "file"-"Save"
If you only disable right-click and select copy, you can also use "file"-"Save as" in the browser menu to copy files. To make the copy invalid, you can
The Code is as follows: |
Copy code |
Add the following code between <body> and </body>: <Noscript> <Iframe src = "*. htm"> </iframe> </Noscript>
|
In this way, when a user saves a webpage, the error "the Web page cannot be saved" will occur.
3. Prohibit Replication
Usage: return false in the oncopy event
The Code is as follows: |
Copy code |
Oncopy = "return false ;" |
1. Prohibit copying webpage content
The Code is as follows: |
Copy code |
<Body oncopy = "return false;"> |
2. Prohibit copying Element Content
The Code is as follows: |
Copy code |
<Input type = "text" name = "username" oncopy = "return false;"/> |
Ii. Disable pasting
Usage: return false in the onpaste event
Onpaste = "return false;" Do not paste content into the element.
The Code is as follows: |
Copy code |
<Input type = "password" name = "pwd" onpaste = "return false;"/> |
Iii. copy and paste source information
In order to prevent the copy party from indicating the source information during reprinting, the copy party must automatically add a copyright notice after the copied content (default latency: 100 milliseconds)
You need to use the clipboardData object:
GetData ("Text") method: obtains the Text information of the clipboard.
SetData ("Text", val) method: sets the Text information of the clipboard.
SetTimeout (code, delay) method: time-out execution of the specified code
The Code is as follows: |
Copy code |
<Html> <Head> <Title> Paste Source Information </title> <Script type = "text/javascript"> Function copysource (){ Var source = clipboardData. getData ("Text "); Source = source + "this article is from Bo www. bKjia. c0m:" + window. location. href; ClipboardData. setData ("Text", source ); } </Script> </Head> <Body oncopy = "window. setTimeout ('copysource () ', 100);"> </Body> </Html> |
Note: The above code passes the test in IE and Chrome, but the right-click of Firefox cannot be used, but the text can still be selected, so for compatibility consideration,
You need to add this attribute to the style of the body:
The Code is as follows: |
Copy code |
-Moz-user-select: none; |
In this way, you don't have to worry about your website content being copied by others.