通過下面的js代碼,可以有效地防止別人直接復制拷貝你的文章,用frame標簽引用你的文章時,會自動跳轉到文章正常鏈接,同時禁止右鍵菜單。下面由WordPress教程欄目給大家介紹具體方法。
使用方法一:
打開當前主題頭部模板header.php找到:將下面代碼添加到后面:
<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標簽引用 if (parent.frames.length > 0) top.location.replace(document.location); </script>
使用方法二:
上面的方法查看源代碼時有些亂,可以在當前主題目錄新建一個名稱為copyright.js文件,將下面代碼添加進去:
//?禁止右鍵 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標簽引用 if?(parent.frames.length?>?0)?top.location.replace(document.location);
然后再將下面代碼添加到當前主題函數模板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'?); }
代碼中加了判斷,管理員登錄狀態一下,防復制代碼無效。
當然上面的方法,也只是忽悠一下小白,瀏覽器禁用JavaScript后,將失去效果。
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END