DIV CSS 佈局教程網

用清空程式碼的方式防止檢視原始碼
編輯:Dreamweaver相關     

以下提供一段「執行/複製/保存代碼」的輔助程式碼,示範如何防止使用者輕易查看原始碼。

程序代碼如下:

<script>
//取得一個物件
function getByid(id){
 if(document.getElementById){return document.getElementById(id);}
 else if(document.all){return document.all[id];}
 else if(document.layers){return document.layers[id];}
 else{return null;}
}
//執行視窗操作 function creatID(DivID){...}
function runCode(obj){...}
//複製代碼 function doCopy(obj){...}
//另存代碼 function saveCode(obj){...}
</script>

核心的防檢視原理在下面的 textarea 範例中:

<textarea id="runcode0" cols="72" rows="9">
<html>
<head>
<script language="javascript">
function clear(){
 Source=document.body.firstChild.data;
 document.open();
 document.close();
 document.title="看不到源代碼";
 document.body.innerHTML=Source;
}
</script>
</head>
<body onload=clear()>
<!-- 這招算是目前網路上公布的防止查看源代碼方法中較好的一種;當然要看仍是有辦法的,例如在網址列輸入 javascript:alert(document.documentElement.outerHTML); -->
</body>
</html>
</textarea>

<input type="button" value="運行代碼" id="0" onClick="runCode(this.id)">
<input type="button" value="複制代碼" id="0" onClick="doCopy(this.id)">
<input type="button" value="保存代碼" id="0" onClick="saveCode(runcode0)">

其原理是:頁面載入時透過 clear() 先把 body 內容暫存,再清空並重寫,讓一般使用者右鍵「檢視原始碼」時看到的是被清空的內容,藉此提高窺探門檻。不過這種方式只能防君子,懂技術的人仍有辦法還原。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved