本文實例講述了javascript判斷並獲取注冊表中可信任站點的方法。分享給大家供大家參考。具體分析如下:
判斷可信任站點,首先要在注冊表中找到可信任站點在注冊表中的位置,如下:
(1)域名作為可信任站點在注冊表中的位置:
HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Domains\\
(2)IP作為可信任站點在注冊表中的位置:
HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\InternetSettings\\ZoneMap\\Ranges
具體測試代碼如下:
index.jsp:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>獲取並判斷可信任站點(域名和IP)</title> <style type="text/css"> .mainContent{ margin: 0 auto; margin-top: 100px; margin-left: 100px; } </style> <script type="text/javascript" src="js/testRegister.js"></script> </head> <body> <div class="mainContent"> <input type="button" value="是否是可信站點" id="testRegister" /> </div> </body> </html>
js代碼:
/* * 判斷可信任站點(可信任站點可以為IP地址也可以為域名) */ window.onload = function(){ var btnObj = document.getElementById("testRegister"); btnObj.onclick = function(){ if(navigator.userAgent.indexOf("MSIE") == -1){ alert("只支持IE浏覽器!"); return; } var hostname = window.location.hostname; var WshShell = new ActiveXObject("WScript.Shell"); //IP的正則表達式 var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/; //根據域名判斷是否存在可信站點 if(hostname != "localhost" && !reg.test(hostname)){ var domainSFlag = false,domainEFlag = false,domainSEFlag = false,domainSSEFlag = true; var hostnamePrefix = "",hostnameSuffix = ""; var indexOf = hostname.indexOf("."); if(indexOf != -1){ hostnamePrefix = hostname.substring(0, indexOf); hostnameSuffix = hostname.substring(indexOf+1, hostname.length); try{ WshShell.RegRead("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\" + hostname + "\\http"); }catch(e){ domainEFlag = true; } if(domainEFlag){ try{ WshShell.RegRead("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\" + hostnameSuffix + "\\" + hostnamePrefix + "\\http"); }catch(e){ domainSFlag = true; } } //判斷其合法性 if(domainEFlag && domainSFlag){ try{ WshShell.RegRead("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\" + hostnameSuffix + "\\" + hostnamePrefix + "\\*"); var tipInfo = "<div>您加入的可信站點不是合法的可信站點,請以<span style='color:red;'>http://</span>開頭!</div>"; alert(tipInfo); return; }catch(e){} } }else{ try{ WshShell.RegRead("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\" + hostname + "\\http"); }catch(e){ domainSEFlag = true; } //判斷其合法性 if(domainSEFlag){ try{ WshShell.RegRead("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\" + hostname + "\\*"); var tipInfo = "<div>您加入的可信站點不是合法的可信站點,請以<span style='color:red;'>http://</span>開頭!</div>"; alert(tipInfo); return; }catch(e){} } } if((domainSFlag && domainEFlag) || domainSEFlag){ var tipInfo = "域名為" + hostname + "的可信任站點不存在!"; alert(tipInfo); alert(tipInfo); return; } }else{ //獲取可信任站點IP,數字2000沒法解釋,主要涉及到注冊表的問題 var str = []; for(var i = 1;i < 2000;i++){ try{ str[i] = WshShell.RegRead("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges\\Range" + i + "\\:Range"); }catch(e){ } } var count = true; for(var i = 1;i < str.length;i++){ if(str[i] == undefined){ continue; }else{ if(str[i] == hostname){ count = false; break; } } } if(count){ var tipInfo = "IP為" + hostname+"可信任站點不存在!"; alert(tipInfo); return } } alert("存在可信任站點!"); } }
希望本文所述對大家的javascript程序設計有所幫助。