问题描述:

So I was looking to write a function that would test if user is using IE / IE9 and conditionally load a separate stylesheet if true. Have read through a ton of documentation but there doesn't seem to be a single aggregated consistent and recent (circa 2011) answer that I could find.

So the questions are:

1) What is your preferred and most modern browser detection method? Checking for document.attachEvent is what I use but is there a better way?

2) What is your preferred way of conditionally changing stylesheets dynamically based on browser? setAttribute doesn't work on <style>, not sure how to do this.

Thanks in advance,

Denis

网友答案:

You are looking for Conditional Comments

<!--[if IE 9]>
your IE9 stylesheet
<![endif]-->
网友答案:

you can do it by many methods

1) CSS check

<!--[if lt IE 7]>your stylesheet for ie < 7 <![endif]-->
<!--[if IE 7]>your stylesheet for ie7<![endif]-->
<!--[if IE 8]>your stylesheet for ie8<![endif]-->
<!--[if IE 9]>your stylesheet for ie9<![endif]-->

2) by javascript check

var isIe = (navigator.appName == "Microsoft Internet Explorer")
var isIe9 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 9") != -1)

3) using third party libray like Modernizr

相关阅读:
Top