如何在客户端判断浏览器的类型(Detecting IE7+ in JavaScript)
2007-06-25 16:20
686 查看
Today I was updating some Javascript code to support the rapidly-approaching Internet Explorer 7. There were a few places in the code where there were IE-specific workarounds, which happily are no longer needed in IE 7
thanks to its improved standards support. Yay position:fixed!
Where the code used to check for if (ie) { ... }, now I wanted it to check for if (ie6OrLower) { ... }. So how to you tell the difference between IE 6 and IE 7+? You could parse the user-agent string, but I’d rather detect changes in the javascript object model. Here’s what I came up with:
if (typeof document.body.style.maxHeight != "undefined") {
// IE 7, mozilla, safari, opera 9
} else {
// IE6, older browsers
}
This distinguishes between browsers based on the fact that IE 7 knows about the maxHeight css property, whereas previous versions of IE didn’t. Does that seem like a sane approach to you?
Update: over at Ajaxian Arjan points out that it’s a bit simpler to check for window.XmlHttpRequest, which is also new in IE 7.
JAVASCRIPT:
if (typeof document.body.style.maxHeight != "undefined") {
// IE 7, mozilla, safari, opera 9
} else {
// IE6, older browsers
}
You can also use the XHR check:
if (window.XMLHttpRequest) { // IE 7, mozilla, safari, opera 9 } else { // IE6, older browsers } If script is executed width , then document.body is not available yet. if (window.XMLHttpRequest) { if(document.epando){ alert("ie7"); //IE7 }else{ //mozilla, safari, opera 9…etc alert("mozilla"); } } else { // IE6, older browsers alert("ie6"); } 偶测试了一下,发现这最后一种方法区别不了 IE7还是firefox,请高人指点一下!
相关文章推荐
- 如何在客户端判断浏览器的类型(Detecting IE7+ in JavaScript)
- 如何用JavaScript判断客户端浏览器是否安装了Flash插件,及其版本?
- 如何判断客户端的浏览器类型和版本号
- 如何根据User Agent参数字符串判断客户端使用的是什么浏览器的版本及类型
- WEB前端——使用Javascript判断客户端浏览器类型
- javascript:如何判断浏览器类型
- 如何用JavaScript判断客户端浏览器是否安装了Flash插件,及其版本?
- 如何用JavaScript判断客户端浏览器是否安装了Flash插件,及其版本
- javascript判断浏览器和终端类型,js如何区分手机、电脑终端和浏览器
- 如何用JavaScript判断客户端浏览器是否安装了Flash插件,及其版本?【转】
- javascript:判断浏览器类型和客户端操作系统 (2009-06-02更新)
- 如何判断客户端的浏览器类型和版本号
- WEB前端——使用Javascript判断客户端浏览器类型
- 如何判断客户端的浏览器类型和版本号
- Javascript 判断客户端浏览器类型代码
- PHP如何判断用户浏览器类型与语言
- JS如何判断浏览器类型和详细区分IE各版本浏览器
- JavaScript判断浏览器类型及版本
- 《如何使用Javascript判断浏览器终端设备》