If browser is IE 6 7 8 9, Firefox, Safari, Chrome or Opera


/ Published in: jQuery
Save to your folder(s)

The HTML simply discovers if it's IE and its version or if it's not IE.
The jQuery discovers if it's IE, Firefox, Webkit Engine (Chrome and Safari) or Opera and its version as an integer.


Copy this code and paste it in your HTML
  1. <!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
  2. <!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
  3. <!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
  4. <!--[if gte IE 8]> <html lang="en" class="no-js ie9"> <![endif]-->
  5. <!--[if !IE]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
  6. <head>
  7. <title></title>
  8. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" ></script>
  9. <style type="text/css" media="screen">
  10.  
  11. html { background: green; }
  12.  
  13. html.ie7 { background: blue; }
  14. html.ie8 { background: yellow; }
  15. html.ie9 { background: red; }
  16.  
  17. </style>
  18. <script type="text/javascript" >
  19. $(document).ready(function(){
  20.  
  21. if ( $.browser.msie )
  22. {
  23. alert( 'This is a Microsoft Internet Explorer ' + $.browser.version.substr(0, 1) );
  24. }
  25. else if ( $.browser.mozilla )
  26. {
  27. alert( 'This is a Mozilla Firefox ' + $.browser.version.substr(0, 1) );
  28. }
  29. else if ( $.browser.webkit || $.browser.safari )
  30. {
  31. alert( 'This is a Webkit Engine Browser (Apple Safari or Google Chrome) with version ' + $.browser.version.substr(0, 1) );
  32. }
  33. else if ( $.browser.opera )
  34. {
  35. alert( 'This is an Opera ' + $.browser.version.substr(0, 1) );
  36. }
  37.  
  38. })
  39. </script>
  40. </head>
  41. <body>
  42. <p>
  43. default: green
  44. <br/>
  45. ie7: blue
  46. <br/>
  47. ie8: yellow
  48. <br/>
  49. ie9: red
  50. </p>
  51. </body>
  52. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.