一、判斷是否IE瀏覽器(支持判斷IE11與edge)
function IEVersion() {
var userAgent = navigator.userAgent; //取得瀏覽器的userAgent字符串
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判斷是否IE<11瀏覽器
var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判斷是否IE的Edge瀏覽器
var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
if(isIE) {
var reIE = new RegExp("MSIE (\d+\.\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if(fIEVersion == 7) {
return 7;
} else if(fIEVersion == 8) {
return 8;
} else if(fIEVersion == 9) {
return 9;
} else if(fIEVersion == 10) {
return 10;
} else {
return 6;//IE版本<=7
}
} else if(isEdge) {
return 'edge';//edge
} else if(isIE11) {
return 11; //IE11
}else{
return -1;//不是ie瀏覽器
}
}
也可以加下對chrome和firebox的判斷:
<script type="text/JAVAscript">
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
if (window.ActiveXObject)
Sys.ie = ua.match(/msie ([d.]+)/)[1]
else if (document.getBoxObjectFor)
Sys.firefox = ua.match(/firefox/([d.]+)/)[1]
else if (window.MessageEvent && !document.getBoxObjectFor)
Sys.chrome = ua.match(/chrome/([d.]+)/)[1]
else if (window.opera)
Sys.opera = ua.match(/opera.([d.]+)/)[1]
else if (window.openDatabase)
Sys.safari = ua.match(/version/([d.]+)/)[1];
//以下進行測試
if(Sys.ie) document.write('IE: '+Sys.ie);
if(Sys.firefox) document.write('Firefox: '+Sys.firefox);
if(Sys.chrome) document.write('Chrome: '+Sys.chrome);
if(Sys.opera) document.write('Opera: '+Sys.opera);
if(Sys.safari) document.write('Safari: '+Sys.safari);
</script>
二、加入首頁和加入收藏、保存到桌面 (未驗證)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/JavaScript">
//設為首頁
function SetHome(obj, url) {
try {
obj.style.behavior = 'url(#default#homepage)';
obj.setHomePage(url);
} catch (e) {
if (window.netscape) {
try {
netscape.security.PrivilegeManager
.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("抱歉,此操作被瀏覽器拒絕!nn請在瀏覽器地址欄輸入“about:config”并回車然后將[
signed.Applets.codebase_principal_support]設置為'true'");
}
} else {
alert("抱歉,您所使用的瀏覽器無法完成此操作。nn您需要手動將【" + url + "】設置為首頁。");
}
}
}
//收藏本站
function AddFavorite(title, url) {
try {
window.external.addFavorite(url, title);
} catch (e) {
try {
window.sidebar.addPanel(title, url, "");
} catch (e) {
alert("抱歉,您所使用的瀏覽器無法完成此操作。nn加入收藏失敗,請使用Ctrl+D進行添加");
}
}
}
//保存到桌面
function toDesktop(sUrl, sName) {
try {
var WshShell = new ActiveXObject("WScript.Shell");
var oUrlLink = WshShell.CreateShortcut(WshShell
.SpecialFolders("Desktop")
+ "\" + sName + ".url");
oUrlLink.TargetPath = sUrl;
oUrlLink.Save();
} catch (e) {
alert("當前IE安全級別不允許操作!");
}
}
</script>
</head>
<body>
<a href="javascript:void(0);" οnclick="SetHome(this,'http://www.baidu.com');">設為首頁</a>
<a href="javascript:void(0);" οnclick="AddFavorite('我的網站',location.href)">收藏本站</a>
<a href="javascript:void(0);" οnclick=" toDesktop(location.href,'我的網站')">保存到桌面</a>
</body>
</html>