用 javascript 脚本 防止搜索引擎抓取页面上的敏感信息 (jquery)

为了防止在搜索引擎搜索到不想公开的信息如:邮箱,电话,但是又想在相应的页面显示,除了设置爬虫屏蔽整个页面,还有一个比较通用的方法利用js重写内容

test.php 代码如下:

 <p class=”hex”><?=bin2hex(”+86(10) 85911014-13″)?></p>
<p class=”hex”><?=bin2hex(”wangchao123.com@gmail.com)?></p>
<p class=”hex”><?=bin2hex(”会员姓名”)?></p>

ps:   为所有需要替换的标签绑定 hex 样式以便jQuery选择器选择 (bin2hex 将字符串专函为 ASCII码)

 

jQuery 代码部分:

<script language=”javascript”>

//加载完成后替换所有 hex 的地方
$(function (){ 
 $(”.hex”).each(function(){
  var oldtext = $(this).text();
   $(this).text(hex2bin(oldtext));
 })
})

//ascii 码,字符串互转的方法
function hex2bin(hex){
 var result = “”;
 if(hex && hex.length && hex.length % 2 == 0){

  for(var i = 0 ;i<hex.length;i+=2){
   result += “%”;
   result += hex.substr(i, 2);
  }
  result = decodeURIComponent(result);
 }
 return result;
}

function bin2hex(bin){
 var result = “”;
 var temp = “”;
 for(var i=0;i<bin.length;i++){
  var chr = bin.charCodeAt(i);
  if(chr>127){
   chr = encodeURIComponent(bin.charAt(i));
  }else{
   chr = chr.toString(16);
   if(chr.length == 1){
    chr = “0″ + chr;
   }
  }
  result += chr;
 }

 for(var i=0;i<result.length;i++){
  var chr = result.charAt(i);
  if(chr!=’%'){
   temp+=chr;
  }
 }
 return temp.toLowerCase();
}

</script>

输出结果页html源代码:

<p align="left" class="hex">2b3836283130292038353931313031342d3138</p>
<p align="left" class="hex">2b383628313029203133393130313535383735</p>
<p align="left" class="hex">2b3836283130292038353931313031342d3138</p>
实现了不让搜索引擎抓取敏感信息

(标题未注明“转载”的为“原创”,转载请注明来源,欢迎来信交流 wangchao123.com@gmail.com)

, ,

  1. 还没有任何评论。
(will not be published)

回到顶端 TOP

mian:103234 ___43 queries ___ 1.093 seconds.