How to write IE Version specific jQuery code

Well, making code browser compatible is always a challenge and presence of various browsers makes life difficult for developers. In this post, you will find the way to code for IE specific version. jQuery provides .browser property which returns the browser information. Below jQuery code checks for IE version 7 only.
$(document).ready(function(){
  if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 7)
  {
      //Your Code Goes Here...
  }
});​
The above code first checks if the browser is IE or not using "jQuery.browser.msie". This will return true for IE and false for other browsers. And after that it checks for the version of the browser.

See result below. (Please check in IE only)



You can also check for IE 8 or 9. All you need to change is the value in the if condition. Currently it is checking only for IE 7. So for IE 8,
$(document).ready(function(){
  if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 8)
  {
      //Your Code Goes Here...
  }
});​
If you want to run the code for IE 7 or higher version of IE, then just change the condition a little bit. See below.
$(document).ready(function(){
  if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) >= 7)
  {
      //Your Code Goes Here...
  }
});​
Feel free to contact me for any help related to jQuery, I will gladly help you.
SHARE

Nothing shocks me, I'm a Software Engineer. And I am not young enough to know everything. I live in World Wide Web and from there take care of this website. This website communicates about my work, learning and experience. I believe life is short, and it is for loving, sharing, learning and connecting. So lets connect..