Disable the web page right-click menu , but you can still copy using shortcut keys."/> Disable the web page right-click menu , but you can still copy using shortcut keys.">

Home  >  Article  >  Web Front-end  >  Detailed explanation of how JavaScript prevents browser pages from copying function codes

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

伊谢尔伦
伊谢尔伦Original
2017-07-22 15:05:482462browse

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,

48084aadf5cda8012766393a53206609Disables 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>

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>

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; 
} 
}


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!

Statement:
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