13

I am trying to target IE7 with an if statement in a jQuery function. My code to this specific bit is:

if($.browser.msie && $.browser.version.substring(0) == "7") {
    //Do something
}

Is this correct?

Josh Unger
  • 6,717
  • 6
  • 33
  • 55
mtwallet
  • 5,040
  • 14
  • 50
  • 74

1 Answers1

37

Try:

if($.browser.msie && parseInt($.browser.version, 10) == 7) {
    //Do something
}   

And as @Andrew Whitaker comments, instead of targeting a specific browser, consider detecting features instead.

karim79
  • 339,989
  • 67
  • 413
  • 406
  • Agreed with that statement. Even the jquery apis says the following: "We recommend against using this property, please try to use feature detection instead." (http://api.jquery.com/jQuery.browser/) – Tjirp Dec 16 '10 at 17:38
  • 4
    @Tjirp, I don't think there is a way to detect the 'feature' for "IE7 draws scrollbars inside content window on overflow, IE8 draws them outside". – CaffGeek Dec 06 '11 at 16:23
  • Nice script, upvote. However, what if I wanted to target IE7 and IE8? I tested with `if($.browser.msie && parseInt($.browser.version, 10) == 7,8)` and it works. However, my JSLint tells me there's an error. I'm not a JavaScript guru so I don't understand what the error means. My idea is to DYR so I don't have to repeat the same function for two different browsers. Any idea how to accomplish this? Thanks. – Ricardo Zea Nov 19 '12 at 17:00
  • `$.browser` is now removed from jQuery >= 1.9, so this no longer works. – varatis Aug 13 '14 at 18:51