JavaScript禁止頁面操作的範例程式碼

來源:互聯網
上載者:User

JavaScript禁止頁面操作的範例程式碼

 本篇文章是對JavaScript禁止頁面操作的範例程式碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所協助

單的JS禁止頁面右鍵菜單--避免網站資訊被盜用 

代碼如下:

<script type="text/javascript">

  function block(oEvent){

   if(window.event)

    oEvent=window.event;

   if(oEvent.button==2)

    alert("滑鼠右鍵不可用");

  }

  document.onmousedown=block;

</script>

 

網頁上禁止複製主要靠JavaScript來實現。

 

<BODY oncontextmenu="return false" onselectstart="return false" 

ondragstart="return false" onbeforecopy="return false" oncopy=document.selection.empty() onselect=document.selection.empty()> 

-----------------------------------------------

防止複製的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>

 

-------------------------------------------------------

防止下載的js

-------------------------------------------------------

<noscript><iframe src=""></iframe></noscript>

--------------------------------------------------------

 

下面是防止右鍵的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禁止選擇和複製</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>

這裡是網頁內容,試試您能不能複製?

</body>

</html>

 

 

應用執行個體:

代碼如下:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>JS控制頁面</title>

    <script language="javascript" type="text/javascript">

        //禁止使用者使用郵件(隱藏右鍵菜單)

        document.oncontextmenu = function forbidRightKey() {

            window.event.returnValue = false;

         }

         //禁止使用者使用郵件(提示使用者禁用右鍵)

         function block() {

             var oEvent;

             if (window.event)

                 oEvent = window.event;

             if (oEvent.button == 2)

                 alert("滑鼠右鍵不可用");

         }

         document.onmousedown = block;

 

        //當使用者按鍵AltF4的時候,提示關閉視窗資訊

        function altF4() {

            if (window.event.altKey && window.event.keyCode == 115) {

                  window.close();

            }

        }

        //alt 滑鼠左鍵 組成快速鍵 選中文字框中內容 調用方式<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 = "輸入搜尋條件";

        }

        //禁用鍵盤上的某幾個鍵 調用onkeydown="ForbidKeys();"

        function ForbidKeys() {

            var content = window.event.keyCode;

            if (content >= 65 && content <= 68) {

                window.event.returnValue = false;

            } else {

                window.alert(content);

            }

        }

 

        //禁止複製網頁內容 <body oncopy="forbidCopy();">

         function forbidCopy() {

            window.event.returnValue = false;

            window.clipboardData.setData("Text", "");

            alert("禁止複製網頁內容!");

        }

 

        //設定複製內容 附加 本網站的URL

        function SetCopyContent() {

            window.event.returnValue = false;

            var content = document.title + "/r/n";

            content += document.getElementById("txt1").value + "/r/n";

            content += "本資源來自 " + this.location.href;

            window.clipboardData.setData('Text', content);

            alert("複製成功,請粘貼到你的QQ/MSN上推薦給你的好友");

        }

 

        //禁止重新整理F5 和Ctrl+F5

        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;

        //屏蔽JS錯誤

        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="複製文字框中的值"  onclick="SetCopyContent();" />

        <input id="txtContent" value="輸入搜尋條件" style="color:Aqua" onclick="clearTxtByInput();"  onblur="FillTxtByInput();" />

        <p>content</p>

 

    </div>

    </form>

</body>

</html>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.