網頁不讓用戶復制方法總匯,設置html禁止選擇,保護源碼,js禁止復制文字


這篇文章主要講解:右鍵復制失效方法、菜單"文件"-"另存為"失效方法、防止查看源代碼進行復制的方法、防止頁面緩存的方法。來達到一定的代碼保護效果

 

右鍵復制失效方法:

方法一:

<script language="Javascript">
document.oncontextmenu=new Function("event.returnValue=false");document.onselectstart=new Function("event.returnValue=false");
</script>

 

方法二:

<body oncontextmenu="return false" onselectstart="return false">

 

或者

<body oncontextmenu="event.returnValue=false" onselectstart="event.returnValue=false">

 

方法三:

<body oncopy="alert('對不起,本網頁禁止復制!');return false;">

 

方法四:

body{//通過css設置body
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
}

 

上面方法都是針對整個頁面的,如果只想對某一個div的內容不讓用戶復制,你需要這樣做把body換位對應div

 

菜單"文件"-"另存為"失效方法:  

如果只是禁止了右鍵和選擇復制,別人還可以通過瀏覽器菜單中的"文件"-"另存為"拷貝文件。為了使拷貝失效,可以在<body>與</body>之間加入以下代碼: 

<noscript><iframe src="*.htm"></iframe></noscript>

 

這樣用戶再選擇“另存為”,就會出現"無法保存Web頁"的錯誤

 

防止查看源代碼進行復制的方法:

通過js實現靜止右鍵和F12,Shift+F10,Ctrl+Shift+I等查看源代碼的方法:

<script language="javascript">
    function click(e) {
        if(document.all) {
            
            if(event.button == 2 || event.button == 3) {
                oncontextmenu = 'return false';
            }
        }
        if(document.layers) {
            if(e.which == 3) {
                oncontextmenu = 'return false';
            }
        }
    }
    if(document.layers) {
        document.captureEvents(Event.MOUSEDOWN);
    }
    document.onmousedown = click;
    document.oncontextmenu = new Function("return false;")
    document.onkeydown = document.onkeyup = document.onkeypress = function() {
        //123屏蔽F12,73屏蔽Ctrl+Shift+I,121屏蔽Shift+F10
        if(window.event.keyCode == 123 || window.event.keyCode == 73 || window.event.keyCode == 121) {
            window.event.returnValue = false;
            return(false);
        }
    }
</script>

 

 

 

防止頁面緩存的方法

方法一:通過設置mate

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=8">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">

 

方法二:對不需要緩存的圖片,js等文件地址后添加隨機數即可,例如:

<img src="picture.jpg?1222259157.415" alt="">

 

<script> 
document.write("<script type='text/javascript' src='test.js?"+Math.random();+"'></script>"); 
</script>

 

 

總結:

以上方法只能單純的防止用戶不能復制,起一定的保護源碼的作用,主要只防止不勞而獲的小白。而且瀏覽器很多都自帶有查看網頁源代碼的功能,目前為止這塊是沒辦法做到真正屏蔽的,我們只能通過壓縮混淆加密等方法來處理我們的源碼,能增加閱讀破解的成本。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM