-1

I'm developing a plugin for jQuery, and because of a bug (not sure if it's a bug in IE or in jQuery) I need to detect whether the plugin is used in IE or not.

A comprised version of the background: My plugin needs to handle the current value for the CSS property clip. Normally you'd do something like value = $('#my-element').css('clip');. But if the clip value contains the (permitted) value auto, the return value gets compromised: Every auto gets changed into 0px.

Say, the CSS sets the element to clip: rect(0px, 50px, auto, 0px). In current Firefox, value = $('#my-element').css('clip'); returns correctly rect(0px, 50px, auto, 0px). But in IE I get rect(0px, 50px, 0px, 0px). Of course, this throws everything off course. In my tests all IE version from 8 to 11 showed this problem, as well as the most current versions of jQuery.

Luckily, you get the correct value if you use the IE-specific currentStyle, e. g. value = $('#my-element').get(0).currentStyle.clip. But because it's IE-specific, all the other browsers get tripped by that.

So, I need a way to detect whether my jQuery plugin is run in IE, regardless of version, or not. So far I found that looking for htmls currentStyle is working, along the lines of

if(document.getElementsByTagName("html")[0].currentStyle) {
  // do IE-stuff
} else
{
  // do it the general way
}

But that seems quite wonky and not very elegant to me. Does anybody know about a better solution to detect the Internet Explorer?

  • you can check the browser in use by [getting the user agent](http://stackoverflow.com/questions/9450066/getting-the-user-agent-with-javascript) – Novocaine Sep 25 '14 at 12:47
  • 2
    if(!search){vote = -1;} – l2aelba Sep 25 '14 at 12:48
  • 1
    http://stackoverflow.com/questions/19999388/jquery-check-if-user-is-using-ie – Karl-André Gagnon Sep 25 '14 at 12:48
  • @Novocaine: Checking the user agent is way too complex for this situation. Additionally, the ua can be easily changed in many browsers (for example, there's a Firefox extension for it), so it's unreliable as well. – Henning Kockerbeck Sep 25 '14 at 13:04
  • There's nothing complex about checking the user agent string. It's **very** simple as you can see in the examples given already. Also it shouldn't be your problem if a user decides to change their user agent manually (very few people will actually do this anyway, usually only developers). – Novocaine Sep 25 '14 at 13:09
  • @l2aelba: I'll bite back the flippant response that might be appropriate to your comment. Instead, I ask politely to show me the other questions where my problem has already been addressed. And no, neither the user agent nor $.browser are feasable approaches. – Henning Kockerbeck Sep 25 '14 at 13:09
  • 1
    http://stackoverflow.com/questions/19999388/jquery-check-if-user-is-using-ie ? – l2aelba Sep 25 '14 at 13:13
  • @Novocaine: I'm afraid if the one paying the bills says "a changed user agent IS your problem" (or the often used synonyme "It has to work everywhere for everybody, period"), it IS your problem, whether it should be or not. – Henning Kockerbeck Sep 25 '14 at 13:14
  • @l2aelba: All about user agent, as far as I can see. And as I wrote before, that's not a feasable approach, regardless whose problem it *should* be. – Henning Kockerbeck Sep 25 '14 at 13:17
  • Just because a user can change it does not make checking the user agent not a feasible option. That's what everyone else would use to check for IE...Any other solution (if there even is one) would be a lot more complex. – Novocaine Sep 25 '14 at 13:19
  • http://dowebsitesneedtolookexactlythesameineverybrowser.com/ – Novocaine Sep 25 '14 at 13:21
  • If it were my call, I'd agree that a site does not need to look the same in every browser. I preach that often myself. But in this case it *isn't* my call. And the possibility of a changed user agent is only one problem. If you for example check whether the ua string contains "IE" - who's to say that there isn't a client out there that's got "IE" in its ua, but isn't Internet Explorer? Just too unreliable, sorry. A less complex (in my view, way less complex) solution is described in the end of my question. I hoped for a more elegant way, but it seems there is none. – Henning Kockerbeck Sep 25 '14 at 13:37

2 Answers2

0

Try this :

  if ( $.browser.msie ) {  
      alert( $.browser.version );
  } 
chandra
  • 41
  • 3
0

Was going to post as a comment but makes more sense as an answer I think.

You havent mentioned any good argument why checking the UA is unfeasible. Spoofing UA isnt your problem, at all. Theres no need to get aggressive about it. If you want to target only IE then you need to check for IE and your only method is UA (checking lists of features and then trying to guess the browser is far more fraut than just checking UA and disregarding spoofers). But I'm not interested in getting into a fight about it, the rest of this answer should answer your question.

However, youre already on the right lines of checking features rather than UA, and youre doing it right. Nothing wrong at all in performing a feature detection check and then executing a code branch based on that check.

You dont have to use an if...else if you dont want to but it seems like a good fit here.

I'm not exactly sure of the wider context but it looks like youre only differentiating between browser inconsistencies to grab a specific style, in which case perform this feature detection and create a getStyle or getClip function based on the feature detection and all the rest of your code remains the same.

Matt Styles
  • 2,442
  • 1
  • 18
  • 23
  • Unfortunately, UA spoofing *is* my problem if the customer says it is... It just has to work. And, as written in a comment above, what about any client out there whose UA just happens to fit the RegEx? Safari uses "like Gecko" in its UA, so looking for "Gecko" to identify Gecko-based clients would fail. I wouldn't like to assure that there's no browser on a desktop, smartphone, tablet, smart tv, car entertainment system or anything else, that has for example "like MSIE" in its UA. – Henning Kockerbeck Sep 25 '14 at 13:56
  • For the wider context: The jQuery plugin needs to get the current value of `clip` to do calculations with it (in the end, we're talking `$.animate` here). Because jQuery on IE doesn't return the correct values in some cases (see the question), I need to replace jQuerys `$.css` with a custom function if the plugin is running on IE. And therefore, I need to detect it. It looks like that checking `html`s `currentStyle` is the best way there is, in this situation. – Henning Kockerbeck Sep 25 '14 at 14:00
  • There is loads of information on UA regex's that are right 99%+ of the time, but yeah, its not a great way to approach a problem. Of course, you can always have the same problem if IE (or others) changes how it handles currentStyle or clip. But in terms of feature detection, I'd say you're spot on track. – Matt Styles Sep 25 '14 at 14:34