一、判断是否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>