通過下面的JS代碼,可以有效地防止別人直接復(fù)制拷貝你的文章,用frame標(biāo)簽引用你的文章時(shí),會(huì)自動(dòng)跳轉(zhuǎn)到文章正常鏈接,同時(shí)禁止右鍵菜單。下面由WordPress教程欄目給大家介紹具體方法。
使用方法一:
打開當(dāng)前主題頭部模板header.php找到:<?php wp_head(); ?>將下面代碼添加到后面:
<script> // 禁止右鍵 document.oncontextmenu = function() { return false }; // 禁止圖片拖放 document.ondragstart = function() { return false }; // 禁止選擇文本 document.onselectstart = function() { if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") return false; else return true; }; if (window.sidebar) { document.onmousedown = function(e) { var obj = e.target; if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") return true; else return false; } }; // 禁止frame標(biāo)簽引用 if (parent.frames.length > 0) top.location.replace(document.location); </script>
使用方法二:
上面的方法查看源代碼時(shí)有些亂,可以在當(dāng)前主題目錄新建一個(gè)名稱為copyright.js文件,將下面代碼添加進(jìn)去:
// 禁止右鍵 document.oncontextmenu = function() { return false }; // 禁止圖片拖放 document.ondragstart = function() { return false }; // 禁止選擇文本 document.onselectstart = function() { if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") return false; else return true; }; if (window.sidebar) { document.onmousedown = function(e) { var obj = e.target; if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") return true; else return false; } }; // 禁止frame標(biāo)簽引用 if (parent.frames.length > 0) top.location.replace(document.location);
然后再將下面代碼添加到當(dāng)前主題函數(shù)模板functions.php的最后:
function copyrightpro_scripts() { wp_enqueue_script( 'copyright', get_template_directory_uri() . '/copyright.js', array(), false ); } if (! current_user_can('level_10') ) { add_action( 'wp_enqueue_scripts', 'copyrightpro_scripts' ); }
代碼中加了判斷,管理員登錄狀態(tài)一下,防復(fù)制代碼無(wú)效。
當(dāng)然上面的方法,也只是忽悠一下小白,瀏覽器禁用JavaScript后,將失去效果。