Home > Web Front-end > JS Tutorial > body text

Detailed explanation of how JavaScript prevents browser pages from copying function codes

伊谢尔伦
Release: 2017-07-22 15:05:48
Original
2477 people have browsed it

Disabling the page copy function, disabling the page right-click menu, etc. I believe everyone has encountered it when browsing the web. The following is a detailed introduction to how to implement it using js,

Disables the web page right-click menu, but you can still use shortcut keys to copy.
js code to disable the copy function:

<script type="text/javascript"> 
document.body.onselectstart=document.body.oncontextmenu=function(){ return false;} 
</script>
Copy after login

Note that this code must be placed behind the body element. It will not work if placed in front or inside the head.
Complete: document.body.onselectstart page selection function.
document.body.oncontextmenu page right-click menu.
document.body.ondragstart page content drag and drop function, drag and drop can be copied. It needs to be disabled when copying is prohibited.
document.body.oncopy page content copy function, when disabled, even if you click copy or use shortcut keys, the content in your clipboard is not the content you just copied but the content you previously placed on the clipboard. The contents may be empty.
document.body.oncut page content cutting function, disabling it has the same effect as disabling copying function.
Note: After using the above disabled function, if you can still right-click or copy a corner of the page, it is because your body does not cover the entire page. You can add the following attributes to the body.
leftMargin=0 topMargin=0 style="width: 100%;height: 100%;"
The code to disable the copy function by setting the body attribute is as follows:

<body oncontextmenu="return false" onselectstart="return false" 
ondragstart="return false" oncopy="return false" 
oncut="return false; 
leftMargin=0 
topMargin=0 style="width: 100%;height: 100%;" > 
以下代码是禁用网页另存为但是我测试没有成功,谁知道原因可以在下面给出评论,谢谢。 
<noscript> 
<iframe scr="*.htm"></iframe> 
</noscript> 
</body>
Copy after login

js code example:

//******************** 屏蔽右键 *********************** 
function click(e) { 
if (document.all) { 
if (event.button==1||event.button==2||event.button==3) { 
oncontextmenu=&#39;return false&#39;; 
} 
} 
if (document.layers) { 
if (e.which == 3) { 
oncontextmenu=&#39;return false&#39;; 
} 
} 
} 
if (document.layers) { 
document.captureEvents(Event.MOUSEDOWN); 
} 
document.onmousedown=click; 
document.oncontextmenu = new Function("return false;") 
//******************************************* 
document.onkeydown=function(evt){ 
if(document.selection.createRange().parentElement().type == "file"){ 
return false; 
} 
if ((event.keyCode==116)|| //屏蔽 F5 刷新键 
(event.ctrlKey && event.keyCode==82)){ //Ctrl + R 
event.keyCode=0; 
event.returnValue=false; 
} 
if ((window.event.altKey)&&(window.event.keyCode==115)){ //屏蔽Alt+F4 
return false; 
} 
}
Copy after login


The above is the detailed content of Detailed explanation of how JavaScript prevents browser pages from copying function codes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template