0

Browser detection is a code smell. However, there are times you want to detect/identify the browser a user is using as opposed to feature/support-detection. How do you do browser detection in JQuery 2.0 and higher?

meager
  • 209,754
  • 38
  • 307
  • 315
Phil
  • 2,031
  • 19
  • 43
  • may be `$.browser` with `jquery-migrate` `-->` https://github.com/jquery/jquery-migrate/#readme – Mohammad Adil Jun 14 '13 at 19:12
  • What do you have against http://modernizr.com/? – j08691 Jun 14 '13 at 19:12
  • @pXL: That is an option, is there a way to do it without adding yet another js file or is that the only option? – Phil Jun 14 '13 at 19:12
  • You don't, they removed it for a reason. There are very, very few reasons to do *browser* detection in modern JavaScript. – meager Jun 14 '13 at 19:13
  • @j08691: Modernizr supports only feature detection if not mistaken. I want to do browser identification and not use anything from JQuery 1.9. – Phil Jun 14 '13 at 19:13
  • @meager: Yes, there are few reasons, but when you hit one of those reason... So, I take it that the only way to do it is to use jquery-migrate? – Phil Jun 14 '13 at 19:17
  • @Phil Which browsers do you need to detect? – user1477388 Jun 14 '13 at 19:18
  • @user1477388: Any browser. I have Telerik Kendo UI grid code that uses $.browser and am trying to if possible to always have JQuery 2.0 or higher in my code; and nothing attaching to me removed features of JQuery 1.9 and lower. – Phil Jun 14 '13 at 19:28
  • possible duplicate of [What is the best way to detect a handheld device in jQuery?](http://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-handheld-device-in-jquery) – davidcondrey May 19 '14 at 05:11
  • @dcc: THis is quite an old question. Anyway, it is not a duplicate of the other. This one is talking about desktop browsers and the pre-jQuery 2.0 browser-detecton/sniffing feature. The one you mention is talking about mobile. – Phil May 20 '14 at 19:35

1 Answers1

3

You don't do it with jQuery, because it's not a feature of the library anymore.

So you do it with another library, or yourself. All the details about browser detection and the javascript code are at Quirksmode.

But just don't do it, really. What's your use case?

jods
  • 4,286
  • 11
  • 17
  • I think I will just use JQuery-migrate. Telerik Kendo UI grid code has $.browser (removed in JQuery 1.8) and it crashes since I am using JQuery 2.0.1. I was hoping there is something I could do so as not to use JQuery-migrate, which may encourage other to use the deleted features in JQuery-migrate not available after 2.0. I guess I have to bit that bullet. – Phil Jun 14 '13 at 19:26
  • @Phil: if your problem is Kendo UI I would keep the supported jQuery version until they update. Actually they seem to have done this already since Kendo UI v2013.1.319 requires jQuery 1.9.1. Maybe you should update Kendo instead? – jods Jun 14 '13 at 19:35
  • @jods: Another good point. I have to double check; I am using the latest Kendo and I have that problem. – Phil Jun 14 '13 at 19:37