使用Java脚本浏览器检测和兼容性检查
我试图从用户代理中检测浏览器类型和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();
更改您的window.loacation.href调用下面的代码:
if (UserAgent.compatibilityMode) {
window.location.href = "http://192.168.10.236/iecorrect.html";
} else {
window.location.href = "http://www.apple.com";
}
你写他们喜欢你会写jQuery的:)
另外,你可以使用''强制IE用户退出兼容模式。 – ACarlson1994 2014-10-11 08:18:32
你太棒了!谢谢 – zhtway 2014-10-11 08:22:47
不是问题!祝你好运! – ACarlson1994 2014-10-11 08:25:37
要检查IE和/或IE浏览器的特定版本,你最好使用条件语句。
我其实不想知道IE版本。我只想知道兼容性视图状态。主要的问题是主html不能重定向的Firefox或铬。 – zhtway 2014-10-11 08:15:31
您是否尝试添加以防止兼容模式? – 2014-10-11 08:20:18
你有什么需要这个?为了向用户显示警告,如“升级到更体面的浏览器”或其他内容? – Juri 2014-10-11 08:07:48
我们的网站实际上在几个客户端IE中处于兼容模式,因此将它们重定向到指令页面以从兼容性视图设置中删除。 – zhtway 2014-10-11 08:11:50