编程小贴士

给你的编程提供小点子


js判断ie版本方法

tips1000_2015-06-28_03-49-4

最近有一个项目是以华为的响应式效果为参考的,为了把项目做好,必然要把华为的源代码重头到尾过一遍,在切图工作这已是家常便饭,当中有几点发现,华为是 基于bootstrap布局,运用了bootstrap的幻灯片组件,又进行了一些二次方法添加改造,而这次个过程中又碰触到了另外一个超屌的js判断 ie版本的解决方案,决定将它分享:

jQuery在1.9版本之前,提供了一个浏览器对象检测的属性$.browser,使用率极高。但是在1.9版本发布之后,大家钟爱的这个属性被jQuery无情的抛弃了。大家开始着手寻找$.browser的替代方案。于是各种利用IE bug的检测方法被搜了出来:

// shortest from a Russianvar ie = !-[1,]// Option from Dean Edwards:var ie = /*@cc_on!@*/false// Use the commented line:var ie//@cc_on=1// Variation (shorter variable):var ie = ‘\v’==’v’// Option to Gareth Hayes (former record-holder):var ie = !+”\v1″

还有一些读取navigator.userAgent的方式,在苹果看来这些写法都不够友好,也不容易记忆,在搜索和挑选之后,终于找到了一个容易理解且友好方便的写法!

解决方案

IE知道自身毛病很多,于是提供的一套官方的HTML hack方式:

<!–[if IE]>

// 全部IE版本可见

<![endif]–>

<!–[if IE 6]>

// IE6可见

<![endif]–>

依次等等。

这样的写法在其它浏览器里,完全就是一坨注释而直接遭到无视,但在IE里却不会。IE会分析里面的提到的版本号,并根据版本号确定要不要解析里面的DOM 元素和文本内容。等一下!DOM元素?那岂不是可以使用js来获取里面的DOM元素?反正谁看到了,谁就是IE!于是,国外大神就有了下面的写法:

var isIE = function(){ var b = document.createElement(‘b’)

b.innerHTML = ‘<!–[if IE]><i></i><![endif]–>’

return b.getElementsByTagName(‘i’).length === 1}

这也太巧妙了!首先生成了一个b元素,设置它的innerHTML为一坨只有IE才认识的注释,注释里只有一个空的标签,然后读取里面的出现的元素i的个数是不是等于1,是不是等于1,是不是等于1。。。。

在大苹果看来,这样的写法比其它任何一种都要好。至于为什么生成一个b元素并且里面写一个i元素而不是div或者strong,更多是考虑到前者字节量更小。

检测各个IE版本的方法也就顺理成章了:

var isIE6 = function(){ var b = document.createElement(‘b’)

b.innerHTML = ‘<!–[if IE 6]><i></i><![endif]–>’

return b.getElementsByTagName(‘i’).length === 1}// var isIE7// …

更进一步

在苹果看来,还可以进一步将版本号提取成参数,就能生成一个通用的检测IE版本的函数了:

var isIE = function(ver){ var b = document.createElement(‘b’)

b.innerHTML = ‘<!–[if IE ‘ + ver + ‘]><i></i><![endif]–>’

return b.getElementsByTagName(‘i’).length === 1}if(isIE(6)){ // IE 6}// …if(isIE(9)){ // IE 9}

这样想检测哪个版本都毫无压力。但是,如果只想检测是不是IE,而不关心浏览器版本,那只需要在调用函数的时候,不传递参数即可。

var ie = isIE()

最后依次贴下在各大浏览器里测试代码的截图。

alert(‘ie6:’ + isIE(6) + ‘\n’ + ‘ie7:’ + isIE(7) + ‘\n’ + ‘ie8:’ + isIE(8) + ‘\n’ + ‘ie9:’ + isIE(9) + ‘\n’ + ‘ie:’ + isIE())

tips1000_2015-06-28_03-49-4

113 Responses to “ js判断ie版本方法 ”

  1. me suba说道:

    fC9Az9 Precisely what I was looking for, thanks for posting.

  2. me suba说道:

    ZOxXIW You completed a number of fine points there. I did a search on the theme and found the majority of people will agree with your blog.

  3. I will immediately clutch your rss feed as I can at to find your e-mail subscription hyperlink or e-newsletter service. Do you ave any? Please allow me recognise in order that I may subscribe. Thanks.

  4. best iptv说道:

    Pretty! This was an incredibly wonderful post. Many thanks for providing these details.

  5. no easy feat. He also hit Nicks for a four-yard TD late in the game.

  6. That is really fascinating, You are an excessively professional blogger.

  7. Lung Health说道:

    Only a few blogger would discuss this topic the way you do.,:

  8. Thanks so much for the blog post.Really looking forward to read more. Really Great.

  9. This website was how do I say it? Relevant!! Finally I ave found something that helped me. Thanks!

  10. Oluwadamilare说道:

    user in his/her mind that how a user can know it. So that as why this article is amazing. Thanks!

  11. viagra cvs说道:

    There may be noticeably a bundle to find out about this. I assume you made sure good points in features also.

  12. visit说道:

    Wow, amazing blog layout! How lengthy have you been running a blog for? you made blogging glance easy. The total look of your site is fantastic, let alone the content material!

  13. Your style is very unique in comparison to other people I have read stuff from. I appreciate you for posting when you ave got the opportunity, Guess I all just book mark this web site.

  14. to find something more safe. Do you have any suggestions?

  15. I’а†ve read various exceptional stuff right here. Surely worth bookmarking for revisiting. I surprise how lots try you set to produce this sort of great informative internet site.

  16. ban arduino说道:

    Way cool! Some extremely valid points! I appreciate you penning this article plus the rest of the website is really good.

  17. for details说道:

    Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Actually Magnificent. I am also an expert in this topic so I can understand your hard work.

  18. Woking taxi说道:

    I truly appreciate this article.Thanks Again. Cool.

  19. Outstanding post however I was wondering if you could write a litte more on this subject? I ad be very grateful if you could elaborate a little bit more. Appreciate it!

  20. You generated some decent points there. I looked on-line for that problem and discovered the majority of people will go coupled with with all your internet site.

  21. take a look at说道:

    Thanks a lot for the post.Much thanks again. Want more.

  22. I? not that much of a online reader to be honest but your blogs really nice, keep it up! I all go ahead and bookmark your site to come back later on. Cheers

  23. you got a very excellent website, Glad I observed it through yahoo.

  24. You have made some decent points there. I looked on the

  25. H1B说道:

    Thanks-a-mundo for the post.Much thanks again. Want more.

  26. I think other web-site proprietors should take this web site as an model, very clean and wonderful user genial style and design, as well as the content. You are an expert in this topic!

  27. anchor说道:

    This particular blog is definitely cool and also factual. I have picked helluva interesting advices out of this source. I ad love to visit it every once in a while. Thanks!

  28. Nwokolo说道:

    pretty valuable material, overall I feel this is really worth a bookmark, thanks

  29. Thanks for the blog post.Really thank you! Really Cool.

  30. I think other site proprietors should take this website as an model, very clean and wonderful user genial style and design, as well as the content. You are an expert in this topic!

  31. May you please prolong them a bit from next time? Thanks for the post.

  32. Muchos Gracias for your blog article.Thanks Again. Fantastic.

  33. Eminem说道:

    woh I am glad to find this website through google.

  34. thuc an cho meo说道:

    J aapprecie cette photo mais j aen ai auparavant vu de semblable de meilleures qualifications;

  35. camera giam sat说道:

    This is really interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your wonderful post. Also, I ave shared your site in my social networks!

  36. may bo dam说道:

    Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is wonderful, as well as the content!. Thanks For Your article about sex.

  37. Some truly superb info , Glad I observed this.

  38. Major thanks for the article.Really thank you! Cool.

  39. Some genuinely nice stuff on this website , I enjoy it.

  40. giay nam cong so说道:

    to get my own, personal blog now my site; camping stove bbq

  41. Normally I do not read post on blogs, but I wish to say that this write-up very forced me to check out and do it! Your writing taste has been amazed me. Thanks, very great article.

  42. Thanks again for the article post.Thanks Again. Much obliged.

  43. Really appreciate you sharing this article post.Really looking forward to read more. Much obliged.

  44. sidekick说道:

    Mr That his involvement will prompt Cheap Jerseys to set even higher standards that other international corporations will endorse.

  45. Supermodel Elena说道:

    Some truly nice stuff on this site, I love it.

  46. Looking around While I was surfing yesterday I saw a excellent post about

  47. rating说道:

    Just to let you know your blog looks a little bit unusual on Firefox on my notebook with Linux.

  48. fashion说道:

    I really liked your article.Thanks Again. Want more.

  49. Wow! This could be one particular of the most helpful blogs We have ever arrive across on this subject. Basically Magnificent. I am also an expert in this topic so I can understand your effort.

  50. Investment tips说道:

    This website certainly has all of the info I wanted about thus subject aand didn at know who

  51. I value the post.Thanks Again. Really Cool.

  52. slim shake说道:

    Thanks for the blog article.Much thanks again. Much obliged.

  53. Crochet basico说道:

    Thanks again for the blog.Much thanks again. Great.

  54. Como tejer说道:

    Very good day i am undertaking research at this time and your website actually aided me

  55. Arctic Circle说道:

    Thank you for any other excellent post. Where else may just anyone get that type of information in such an ideal way of writing? I ave a presentation next week, and I am at the look for such info.

  56. online说道:

    Very nice post. I definitely appreciate this website. Keep writing!

  57. more说道:

    What as up, I log on to your blogs on a regular basis. Your humoristic style is awesome, keep up the good work!

  58. college porn说道:

    This is a excellent blog, would you be involved in doing an interview about just how you designed it? If so e-mail me!

  59. visit website说道:

    It as difficult to It as difficult to find knowledgeable folks with this topic, however you sound like do you know what you are dealing with! Thanks

  60. visit website说道:

    Simply a smiling visitor here to share the love (:, btw great style and design.

  61. School admission说道:

    This is very interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your great post. Also, I ave shared your web site in my social networks!

  62. Waec exam说道:

    Wow, wonderful blog layout! How long have you been running a blog for? you make blogging look easy. The whole look of your site is great, let alone the content!

  63. Thorn of Girl Great details is usually located on this net website.

  64. rest аА аБТ•f the аАаб‚Т€а‚ite аАаб‚Т€Т“аАаб‚Т€а‚ also reаА аЂаlly

  65. Im thankful for the article post. Fantastic.

  66. wow, awesome blog.Thanks Again. Much obliged.

  67. Thorn of Girl Excellent data is often found on this world wide web weblog.

  68. That is a good tip particularly to those new to the blogosphere. Simple but very accurate info Thank you for sharing this one. A must read article!

  69. tm objection说道:

    It as great that you are getting thoughts from this piece of writing as well as from our argument made here.

  70. This is one awesome blog article.Much thanks again. Want more.

  71. This is a very good tip particularly to those fresh to the blogosphere. Simple but very precise info Many thanks for sharing this one. A must read article!

  72. Your style is very unique compared to other folks I have read stuff from. I appreciate you for posting when you ave got the opportunity, Guess I all just book mark this site.

  73. Wow, fantastic blog layout! How long have you been blogging for? you made running a blog glance easy. The total glance of your website is excellent, let alone the content material!

  74. negocios说道:

    Simply wanna remark that you have a very decent site, I the design it really stands out.

  75. Meshack说道:

    Our communities really need to deal with this.

  76. Thank you ever so for you blog post.Really looking forward to read more.

  77. Open Heaven说道:

    I truly appreciate this blog.Much thanks again. Keep writing.

  78. It as hard to find knowledgeable people in this particular topic, however, you sound like you know what you are talking about! Thanks

  79. www.softfind.net说道:

    It as difficult to find well-informed people on this topic, however, you seem like you know what you are talking about! Thanks

  80. Really appreciate you sharing this blog. Really Great.

  81. find out more说道:

    Thanks for sharing, this is a fantastic post.Much thanks again. Want more.

  82. dream punta cana说道:

    It as fantastic that you are getting thoughts from

  83. This really answered the downside, thank you!

  84. pretty handy stuff, overall I consider this is well worth a bookmark, thanks

  85. Thank you for your very fantastically clear fine points and feedback from you. sedan dealers san jose

  86. Your style is really unique compared to other folks I ave read stuff from. I appreciate you for posting when you ave got the opportunity, Guess I will just bookmark this blog.

  87. dclm devotionals说道:

    post to let you know. The design and style look

  88. go there说道:

    very good, Are you contemplating taking up sport fishing.

  89. visit website说道:

    you are going to a famous blogger if you are not already.

  90. guest post说道:

    You made a number of nice points there. I did a search on the subject matter and found most folks will have the same opinion with your blog.

  91. over here说道:

    Really enjoyed this post.Thanks Again. Awesome.

  92. have a look at说道:

    There is clearly a lot to identify about this. I assume you made some nice points in features also.

  93. This website was how do I say it? Relevant!! Finally I have found something which helped me. Many thanks!

  94. This particular blog is obviously awesome and also factual. I have picked a bunch of helpful tips out of it. I ad love to go back again and again. Thanks a lot!

  95. Your style is very unique in comparison to other people I have read stuff from. Many thanks for posting when you ave got the opportunity, Guess I will just book mark this site.

  96. Incredible points. Great arguments. Keep up the great spirit.

  97. Help说道:

    Really informative article post.Really looking forward to read more. Really Cool.

  98. Nnamdi说道:

    This very blog is no doubt interesting additionally amusing. I have picked a bunch of useful tips out of this amazing blog. I ad love to visit it over and over again. Thanks a bunch!

  99. to learn more说道:

    Very interesting info !Perfect just what I was searching for! I live in company with a body, a silent companion, exacting and eternal. by Eugene Delacroix.

  100. I really liked your post.Really looking forward to read more. Much obliged.

  101. free ads说道:

    It is actually a nice and helpful piece of info. I am happy that you simply shared this useful info with us. Please keep us up to date like this. Thanks for sharing.

  102. just go to说道:

    You are my inspiration , I have few blogs and often run out from to brand.

  103. see the website说道:

    You can certainly see your enthusiasm within the work you write. The sector hopes for more passionate writers like you who are not afraid to mention how they believe. All the time follow your heart.

  104. Thanks for the post.Thanks Again. Really Great.

  105. du hoc phap说道:

    wonderful points altogether, you just gained a brand new reader. What would you suggest about your post that you made a few days ago? Any positive?

  106. Wonderful blog! I saw it at Google and I must say that entries are well thought of. I will be coming back to see more posts soon.

  107. whether this post is written by him as nobody else know such detailed about my difficulty.

  108. go说道:

    My brother suggested I may like this website. He used to be totally right.

  109. more enjoyable for me to come here and visit more often.

  110. go there说道:

    wonderful points altogether, you simply won a new reader. What may you recommend in regards to your publish that you made a few days in the past? Any positive?

  111. You made some nice points there. I looked on the internet for the issue and found most individuals will approve with your blog.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>