HTML和CSS中如何判断ie各版本浏览器的方法

html-css010

HTML和CSS中如何判断ie各版本浏览器的方法,第1张

html中区分ie各版本的方法为IE有条件注释,不过这种方式只支持到IE9(包括IE9),在IE10之后移除了该特性;不建议使用;

css中判断IE各版本,只能通过css hack方式,针对浏览器的怪癖行为来区分;不建议使用;

通过js处理浏览器的用户代理userAgent字符串来解析出浏览器的版本,虽然这种方式也不建议使用,但是目前能解析出浏览器类型及版本最好的办法了

<script type=“text/javascript”>

function isIE(){

return navigator.appName.indexOf(“Microsoft Internet Explorer”)!=-1 &&document.all

}

function isIE6() {

return navigator.userAgent.split(“”)[1].toLowerCase().indexOf(“msie 6.0″)==“-1″?false:true

}

function isIE7(){

return navigator.userAgent.split(“”)[1].toLowerCase().indexOf(“msie 7.0″)==“-1″?false:true

}

function isIE8(){

return navigator.userAgent.split(“”)[1].toLowerCase().indexOf(“msie 8.0″)==“-1″?false:true

}

function isNN(){

return navigator.userAgent.indexOf(“Netscape”)!=-1

}

function isOpera(){

return navigator.appName.indexOf(“Opera”)!=-1

}

function isFF(){

return navigator.userAgent.indexOf(“Firefox”)!=-1

}

function isChrome(){

return navigator.userAgent.indexOf(“Chrome”) >-1

}

</script>

下面介绍下 js获取客户端浏览器信息

Navigator 对象包含有关浏览器的信息。js就是通过Navigator的属性获取客户端浏览器信息

Navigator 对象属性:

属性

描述

appCodeName 返回浏览器的代码名。

appMinorVersion返回浏览器的次级版本。

appName 返回浏览器的名称。

appVersion 返回浏览器的平台和版本信息。

browserLanguage返回当前浏览器的语言。

cookieEnabled 返回指明浏览器中是否启用 cookie 的布尔值。

cpuClass 返回浏览器系统的 CPU 等级。

onLine 返回指明系统是否处于脱机模式的布尔值。

platform 返回运行浏览器的操作系统平台。

systemLanguage返回 OS 使用的默认语言。

userAgent 返回由客户机发送服务器的 user-agent 头部的值。

userLanguage返回 OS 的自然语言设置。

实现根据浏览器类型自动调用不同CSS。<SCRIPT LANGUAGE="JavaScript">

<!--

if (window.navigator.userAgent.indexOf("MSIE")>=1)

{

//如果浏览器为IE

setActiveStyleSheet("IE.css")

}else{

if (window.navigator.userAgent.indexOf("Firefox")>=1)

{

//如果浏览器为Firefox

setActiveStyleSheet("Firefox .css")

}else{

//如果浏览器为其他

setActiveStyleSheet("qita .css")

}

}function setActiveStyleSheet(title){

document.getElementsByTagName("link")[0].href="style/"+title

}

//-->

</SCRIPT>