javascript 禁止右鍵,禁止複製,禁止粘貼

來源:互聯網
上載者:User

用javascript
禁止右鍵,禁止複製,禁止粘貼

有時自己辛苦半天做的網頁,尤其是一些javascript特效,很容易被人利用查看源檔案而複製。那麼如何才能防止被人查看原始碼呢?我們可以利用event.button特性來做到。下表是event.button屬性的可能取值及含義:
0 沒按鍵
1 按左鍵
2 按右鍵
3 按左和右鍵
4 按中間鍵
5 按左和中間鍵
6 按右和中間鍵
7 按所有的鍵
參照上表,我們可以在<body>和</body>之間加入如下語句:
<script type="text/javascript">
function Click(){
if (event.button!=1){alert('XXXXXXX');}
document.onmousedown=Click;
</script>

<script type="text/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) {alert("
xxxxxxxxxxx"); } }
{
event.cancelBubble = true
event.returnValue = false;
return false;
}

}

document.oncontextmenu = nocontextmenu; // for IE5+
document.onmousedown = norightclick; // for all others
//-->
</script>
 
圖片禁用滑鼠右鍵代碼

<script type="text/javascript">

        var clickmessage="本站圖片禁用右鍵!"

        function disableclick(e) {

            if (document.all) {

                if (event.button==2||event.button==3) {

                    if (event.srcElement.tagName=="IMG"){

                        alert(clickmessage);

                        return false;

                    }

                }

            }

            if (document.layers) {

                if (e.which == 2) {

                    alert(clickmessage);

                    return false;

                }

            }

        }

        function associateimages(){

            for(i=0;i<document.images.length;i++)

                document.images[i].onmousedown=disableclick;

        }

        if (document.all)

            document.onmousedown=disableclick

        else if (document.layers)

            associateimages()

</script> 

功能:禁止右鍵、禁選擇、禁粘貼、禁shift、禁ctrl、禁alt

<script language="JavaScript">
<!--
function key(){
 //禁止Shift
if(event.shiftKey){
window.close();}
//禁止Alt
if(event.altKey){
window.close();}
//禁止Ctrl
if(event.ctrlKey){
window.close();}
return false;}
document.onkeydown=key;
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>

<body onselectstart="return false"; onpaste="return false";> 

如何用用javascript 禁止右鍵,禁止複製,禁止粘貼,做站時常會用到這些代碼:

1. <body oncontextmenu="window.event.returnValue=false" >將徹底屏蔽滑鼠右鍵特效
<table border oncontextmenu=return(false)><td>no</table> 可用於Table 

2. <body onselectstart="return false"> 取消選取、防止複製 javascript技巧

3. onpaste="return false" 不準粘貼技巧

4. oncopy="return false;" oncut="return false;" 防止複製的javascirpt特效

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.