Example code for JavaScript to disable page manipulation

  • 2020-03-30 00:54:17
  • OfStack

Single JS prohibited page right-click menu - to avoid website information theft


<script type="text/javascript">
  function block(oEvent){
   if(window.event)
    oEvent=window.event;
   if(oEvent.button==2)
    alert(" Right mouse button is not available ");
  }
  document.onmousedown=block;
</script>

The prohibition of copying on web pages is mainly implemented by JavaScript.

< BODY oncontextmenu="return false" onselectstart="return false"
Ondragstart ="return false" onbeforecopy="return false" oncopy=document.selection. Empty () onselect=document.selection. Empty ()>
-----------------------------------------------
Prevent copying of js
-----------------------------------------------


<SCRIPT language=JavaScript1.2>
function disableselect(e){
return false}
function reEnable(){return true
}
file://if IE4+
document.onselectstart=new Function ("return false")
file://if NS6
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
</SCRIPT>
<SCRIPT language=JavaScript type=text/JavaScript>
<!--
function MM_reloadPage(init) {    //reloads the window if Nav4 resized
    if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
      document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
    else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</SCRIPT>

-------------------------------------------------------
Prevent downloading of js
-------------------------------------------------------
< Noscript> < Iframe SRC = "" > < / iframe> < / noscript>
--------------------------------------------------------

The following is to prevent right-click js:


<script language="JavaScript">
<!--
if (window.Event) 
document.captureEvents(Event.MOUSEUP); 
function nocontextmenu() {
event.cancelBubble = true
event.returnValue = false;
return false;
}
function norightclick(e) {
if (window.Event){
   if (e.which == 2 || e.which == 3)
   return false;
}else{
   if (event.button == 2 || event.button == 3){
       event.cancelBubble = true
    event.returnValue = false;
       return false;
    }
}
}
document.oncontextmenu = nocontextmenu; // for IE5+
document.onmousedown = norightclick; // for all others
//-->
</script>
<title>CSS Select and copy are not allowed </title>
<meta http-equiv="content-Type" content="text/html;charset=gb2312">
<style>
body{
 -moz-user-select:none;
 hutia:expression(this.onselectstart=function(){return(false)});
}
</style>
</head>
<body>
 Here is the content of the web page. Can you copy it? 
</body>
</html>

Application examples:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>JS Control page </title>
    <script language="javascript" type="text/javascript">
        //Disable users from using email (hide right-click menu)
        document.oncontextmenu = function forbidRightKey() {
            window.event.returnValue = false;
         }
         //Disable users from using email (prompt users to disable right-click)
         function block() {
             var oEvent;
             if (window.event)
                 oEvent = window.event;
             if (oEvent.button == 2)
                 alert(" Right mouse button is not available ");
         }
         document.onmousedown = block;

        //When the user presses the AltF4, the window information is prompted to close
        function altF4() {
            if (window.event.altKey && window.event.keyCode == 115) {
                  window.close();
            }
        }
        //Alt left mouse button composition shortcut select the text box in the content of the call mode <Body onkeydown = "altF4 ();">
        function getTxtSelect(eventControl) {
            if (eventControl.altKey) {
                document.getElementById("txt1").select();
            }
        }
        function clearTxtByInput() {
            document.getElementById("txtContent").value = "";
        }
        function FillTxtByInput() {
            document.getElementById("txtContent").value = " Enter search criteria ";
        }
        //Disable certain keys on the keyboard to call onkeydown="ForbidKeys();"
        function ForbidKeys() {
            var content = window.event.keyCode;
            if (content >= 65 && content <= 68) {
                window.event.returnValue = false;
            } else {
                window.alert(content);
            }
        }
        //Copying web content is prohibited. Body oncopy = "forbidCopy ();">
         function forbidCopy() {
            window.event.returnValue = false;
            window.clipboardData.setData("Text", "");
            alert(" Do not copy web content! ");
        }

        //Set the URL of this website to copy the content
        function SetCopyContent() {
            window.event.returnValue = false;
            var content = document.title + "/r/n";
            content += document.getElementById("txt1").value + "/r/n";
            content += " This resource comes from  " + this.location.href;
            window.clipboardData.setData('Text', content);
            alert(" Copy successful, please paste into yours QQ/MSN Recommend it to your friends ");
        }
        //Refresh F5 and Ctrl+F5 are not allowed
        function ForbidFreshPage() {
            if ((window.event.ctrlKey && window.event.keyCode == 116) || window.event.keyCode == 116) {
               window.event.keyCode = 0;
               window.event.returnValue = false;
           } 
        }
        document.onkeydown = ForbidFreshPage;
        //Blocking JS error
        function killErrors() {
            return true;
        }
        window.onerror = killErrors;
        
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="txt1" type="text" value="Hello World!"  onclick="getTxtSelect(event)"/>
        <input type="button" value=" Copy the value in the text box "  onclick="SetCopyContent();" />
        <input id="txtContent" value=" Enter search criteria " style="color:Aqua" onclick="clearTxtByInput();"  onblur="FillTxtByInput();" />
        <p>content</p>

    </div>
    </form>
</body>
</html>


Related articles: