我试图从用户代理中检测浏览器类型和IE兼容性并相应地重定向。它只适用于IE,但不适用于Firefox和Chrome。我仍在四处寻找解决方案,但尚未弄清楚。使用Java脚本浏览器检测和兼容性检查
<html>
<head>
<title>Testing </title>
<script src="UserAgent.js" type="text/javascript"></script>
</head>
<body>
<div id="results">Output</div>
<script type="text/javascript">
if (UserAgent.firefox){
window.location.href("http://www.yahoo.com");
}
if (UserAgent.compatibilityMode) {
window.location.href("http://192.168.10.236/iecorrect.html");
} else {
window.location.href("http://www.apple.com");
}
</script>
</body>
</html>
UserAgent.js
if (typeof jQuery == 'undefined') {
document.write("\<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'>\<\/script>");
}
// Create new object
var UserAgent = {
init: function() {
// Get the user agent string
var ua = navigator.userAgent;
this.compatibilityMode = false;
this.firefox = ua.toLowerCase().indexOf("firefox") > -1;
this.chrome = ua.toLowerCase().indexOf("chrome") > -1
if (ua.indexOf("MSIE 7.0") > -1) {
this.compatibilityMode = true;
}
}
};
// Initialize the object
ieUserAgent.init();
你有什么需要这个?为了向用户显示警告,如“升级到更体面的浏览器”或其他内容? – Juri 2014-10-11 08:07:48
我们的网站实际上在几个客户端IE中处于兼容模式,因此将它们重定向到指令页面以从兼容性视图设置中删除。 – zhtway 2014-10-11 08:11:50