12

As many already posted in other questions (also in jQuery documentation), the old jQuery.browser.version is deprecated and works only in jquery1.3.

Do you know another simple way to detect it, that I can include in my code before:

function handleInfoDivPopupVisibility(dynamicEleId, staticEleId){
var parentContainer = $('headerSummaryContainer');
var dynamicEle = $(dynamicEleId);
var staticEle = $(staticEleId);

if(isIE() && parentContainer){
    if (jQuery.browser.version != 10) { // I need to find a way to detect if it's IE10 here.
        parentContainer.style.overflow = 'visible'; 
    }
}
dynamicEle ? dynamicEle.style.display = '' : '';
if(dynamicEle && staticEle)
    gui_positionBelow(dynamicEle, staticEle);
}

Before you say it's duplicated question of this or this, I'd like to reinforce that I don't want to use css hacks. Is there a way to detect it just as simple as I could do before?

if (jQuery.browser.version != 10) {...
Community
  • 1
  • 1
periback2
  • 1,449
  • 3
  • 18
  • 36
  • possible duplicate of [How do I target only Internet Explorer 10 for certain situations like Internet Explorer-specific CSS or Internet Explorer-specific JavaScript code?](http://stackoverflow.com/questions/9900311/how-do-i-target-only-internet-explorer-10-for-certain-situations-like-internet-e) – dsgriffin May 03 '13 at 20:20
  • 1
    @Zenith perhaps you should read all the question: Before you say it's duplicated question of this or this, I'd like to reinforce that I don't want to use css hacks. Is there a way to detect it just as simple as I could do before? – periback2 May 03 '13 at 20:22
  • I did read it. You should read the answers on that question. There are plenty of answers there that may interest you, some hacks, some other alternatives.. – dsgriffin May 03 '13 at 20:23
  • @MikeChristensen, didn't u read the code? what do you think I tried when I did: jQuery.browser.version != 10 ? – periback2 May 03 '13 at 20:24
  • 3
    You could use Modernizr. – Pointy May 03 '13 at 20:24
  • 1
    it's deprecated since jQuery 1.3... – periback2 May 03 '13 at 20:24
  • 5
    It appears as though you're just trying to fix a css issue that affects IE<10. Why not just fix the css issue with css/html structure rather than using javascript and/or browser sniffing? – Kevin B May 03 '13 at 20:24
  • @Pointy, I'm trying to make sure there's a way to do that without having to use CSS hacks... Just as simple as it was possible before... – periback2 May 03 '13 at 20:26
  • 1
    @KevinB, this issue doesn't happend in IE9 or lower... neither in Chrome or FF... Only in IE10 – periback2 May 03 '13 at 20:27
  • This is the point of my question, @Zenith... I want to make sure there's no simpler way to do this... – periback2 May 03 '13 at 20:28
  • What's the actual issue you are having? I strongly recommend using css/html techniques as @KevinB suggested rather than sniffing for the browser version. It's extremely unreliable. Use ***feature detection***. – War10ck May 03 '13 at 20:29
  • 4
    What is the issue, maybe we can help you solve the issue rather than work around it, that way if it also affects IE11, but then not 12, you don't have a bunch of if statements everywhere (and you won't have to touch your code to update it) – Kevin B May 03 '13 at 20:29
  • 2
    @Jan Dvorak, in reply to your comment on the deleted answer - http://msdn.microsoft.com/en-us/library/ie/hh801214(v=vs.85).aspx – dsgriffin May 03 '13 at 20:31
  • 1
    @Zenith thanks. TIL... – John Dvorak May 03 '13 at 20:33
  • 1
    I agree with you all... but this is not an easy code to maintain, as it was written more then 10 years ago... and if I try to refactor the code, it will take too much time and there are many risks of breaking lots of other parts of the code... :/ – periback2 May 03 '13 at 20:33
  • 2
    But what is the actual problem??? We still have yet to see it. Rather than use some buggy unreliable user agent sniffing technique you could probably fix the real issue with relative ease. – War10ck May 03 '13 at 20:35
  • Modernizr is a JavaScript library. How is that a "CSS hack"? – Pointy May 03 '13 at 20:37
  • 1
    I think you've all beaten the horse to death: "_YES, broswer detection is bad_." I think the OP is more than aware of your opinions. He's looking for an answer, not a lecture. This happens on every single question about browser detection... Sometimes people want to do things that _aren't_ the best practice... Sometimes they have reasons. – jahroy May 03 '13 at 20:37
  • @jahroy, several people are offering to go above and beyond, they just want to know what the actual problem is so they can help. My suspicion is that this browser sniff is trying to counteract some other decade-old browser sniff that is no longer needed in IE10. – Dave Methvin May 04 '13 at 01:52
  • Yes, there have been about a half-dozen offers to go "_above and beyond_" and the OP has not responded to any of them. How many times do we really need to ask? – jahroy May 04 '13 at 02:07

6 Answers6

17

In general it's a bad idea to check for browser version, it's considered a better practice to check for browser features. But if you're sure what you're doing:

function getIEVersion(){
    var agent = navigator.userAgent;
    var reg = /MSIE\s?(\d+)(?:\.(\d+))?/i;
    var matches = agent.match(reg);
    if (matches != null) {
        return { major: matches[1], minor: matches[2] };
    }
    return { major: "-1", minor: "-1" };
}

var ie_version =  getIEVersion();
var is_ie10 = ie_version.major == 10;

We have the following code in production, so it works and well-tested.

And yes, we did have a need to detect IE10, not just a particular feature that exists in IE10 but not in earlier versions.

J0HN
  • 23,930
  • 5
  • 46
  • 83
  • 1
    I agree with you.. and this is probably the reason jQuery recommends to use $.support, instead of the deprecated $.browser.version – periback2 May 03 '13 at 20:35
  • I was going to use your answer, but I checked my code and I found a function a little simpler than yours, that was already written here and worked well, but it as it's the same approach you used, I'm also going to accept your answer. Thank you very much. the other guys wanted to know specifically the problem, but it's really hard to identify, because this is an application written initially 10 years ago, and at that time, it was only validated for IE... lots of issues are being fixed now to work cross-browsing (and several hacks were written many years ago for IE)...so you can have an idea.. – periback2 May 06 '13 at 12:09
  • 3
    well, we've got the same situation :) App written 10 years ago, was intended to support only IE and as the part of the current project we were supposed to add cross-browser support, modern styling, MVC, etc. So I perfectly understand your situation :) – J0HN May 06 '13 at 13:38
  • thank you @JOHN :)... sometimes I don't understand people here in SO... If I try to describe well the problem, it requires too much things to write and then they complain is TLDR. when I ask just how to do something, like in this case, I'm not trying to get help to refactor old cold (I don't have time for it)... I just need to know this or that information... they should focus on the question... Some moderator added that ridiculous link before my question pointing to another question totally different from what I asked... but well, you understood my point and helped me. thanks! – periback2 May 06 '13 at 13:47
15

Internet Explorer has the feature of Conditional Compilation (http://www.javascriptkit.com/javatutors/conditionalcompile.shtml). You can use this:

var isIE10 = false;
/*@cc_on
    if (/^10/.test(@_jscript_version)) {
        isIE10 = true;
    }
@*/
alert(isIE10);

DEMO: http://jsfiddle.net/X3Rvz/1/

You can put this before all your JavaScript code, and from then on just reference isIE10.

The conditional compilation won't run in non-IE, so isIE10 will still be false. And @_jscript_version will only start with 10 in IE 10.

Conditional Comments aren't supported in IE 10, and the User-Agent string can be spoofed.

Since minification usually removes comments, you can use eval or Function to find out in a similar fashion:

var isIE10 = false;
if (Function('/*@cc_on return /^10/.test(@_jscript_version) @*/')()) {
    isIE10 = true;
}

DEMO: http://jsfiddle.net/wauGa/2/


UPDATE:

To still avoid minification of comments but also combine detecting any version, you can use something like:

var IE = (function () {
    "use strict";

    var ret, isTheBrowser,
        actualVersion,
        jscriptMap, jscriptVersion;

    isTheBrowser = false;
    jscriptMap = {
        "5.5": "5.5",
        "5.6": "6",
        "5.7": "7",
        "5.8": "8",
        "9": "9",
        "10": "10"
    };
    jscriptVersion = new Function("/*@cc_on return @_jscript_version; @*/")();

    if (jscriptVersion !== undefined) {
        isTheBrowser = true;
        actualVersion = jscriptMap[jscriptVersion];
    }

    ret = {
        isTheBrowser: isTheBrowser,
        actualVersion: actualVersion
    };

    return ret;
}());

And access the properties like IE.isTheBrowser and IE.actualVersion (which is translated from internal values of JScript versions).

Ian
  • 46,701
  • 13
  • 94
  • 107
  • 3
    [This answer](http://stackoverflow.com/a/15657729/778118) suggests you a workaround if you're minifying your script... – jahroy May 03 '13 at 20:33
  • @jahroy Good point, thanks for pointing that out. I remember answering this same question several times before ("How to target IE 10?"), and I swear my answer included using `Function()` which is like the same as `eval`. I just can't find that answer... – Ian May 03 '13 at 20:36
  • @Ian you could also do it with IE conditional comments wrapped around script tags. – Pointy May 03 '13 at 20:38
  • @Pointy But doesn't IE 10 not support conditional comments? Or is it special when inside script tags? – Ian May 03 '13 at 20:40
  • 2
    @Pointy [HTML conditional comments no longer exist as of IE10](http://msdn.microsoft.com/en-us/library/ie/hh801214(v=vs.85).aspx). – Matt Kantor May 03 '13 at 20:40
  • Ian well that's correct, but the OP wants to know if it's IE and *not* IE10. If the comments detect IE, then ipso facto it's not IE10. (Trying to detect IE *but also* skip IE10 is borderline nonsensical, because IE10 is so significantly different than older versions.) – Pointy May 03 '13 at 20:42
  • @Pointy Ahh I see - I think I focused on the title of the OP. But at the same time, the fact that a conditional comment doesn't execute just means that it **isn't** IE <= 9. For example, if you visited the site in Chrome, the conditional comment wouldn't work and it would be regarded as IE 10. Is that right, or am I misunderstanding? – Ian May 03 '13 at 20:45
  • @Ian yes you're right about that, but given that the OP is talking about ancient code, it's probably correct to treat IE10 just like Chrome, since as far as decade-old code is concerned they're both equally alien, and old-IE hacks are probably equally irrelevant. – Pointy May 03 '13 at 20:46
  • @Pointy I see, I guess I just don't want to assume too much. You're welcome to post an answer explaining that because it seems right; I'm just gonna stick with specifically targeting IE 10. – Ian May 03 '13 at 20:50
  • I tested your jsfiddle but it returns true for IE9, and error for IE8/7 :( – periback2 May 03 '13 at 21:08
  • @periback2 Not sure of the problem. It alerts `false` for me in IE9. I can't test in IE7/8 but that's because I don't have those browsers and jsFiddle doesn't work in IE7/8. Are you testing in the actual browsers? Or are you in IE10 and changing the browser mode from Developer Tools or something? Anyways, I updated my answer with a new version (minor change), and added a jsFiddle for my first example. – Ian May 03 '13 at 21:25
  • if you press F12 in IE, the web dev tool bar will open and you'll be able to change to IE9, 8, 7.. and test... for IE9 it returns true :( – periback2 May 06 '13 at 11:22
  • @periback2 Well in that case, you can use `return document.documentMode === 10;` in the function instead of the `return` I already have. – Ian May 09 '13 at 13:58
3

The jQuery.browser.version still works but you have to include the jquery-migrate plugin.

http://api.jquery.com/jQuery.browser/ https://github.com/jquery/jquery-migrate/#readme

0

The IE10 User-Agent String says

However if your site is still using user-agent sniffing, then increasing the “MSIE” token to “10.0” is particularly noteworthy. Why? Because it adds an extra digit to the string value of the token. Most sites will handle this effortlessly, but some will process the extra digit incorrectly, causing them to identify IE10 as IE1.

To help illustrate, here’s a regular expression that only captures the first digit of the “MSIE” token’s value:

// INCORRECT: will report IE10 version in capture 1 as "1"
var matchIE = /MSIE\s(\d)/;

And here’s one that captures the full value of the “MSIE” token:

// Correct: will report IE10 version in capture 1 as "10.0"
var matchIE = /MSIE\s([\d.]+)/
Community
  • 1
  • 1
Mike Samuel
  • 109,453
  • 27
  • 204
  • 234
0

Here is a one line solution to detect IE 10

var IE10 = navigator.userAgent.toString().toLowerCase().indexOf("trident/6")>-1;

and for more information on other version and browsers please refer this Link of Browser Detection

MarmiK
  • 5,320
  • 6
  • 34
  • 44
gulawaiz
  • 11
  • 2
0

I Found myself having a issue (border radius on frameset with legend) with IE 9 through 11 (did not check IE 12)
MSIE is no long user agent in IE 11 and the appName is Mozilla, however trident is in there I managed to extract the trident version # and detect it

if(navigator.appVersion.indexOf('Trident/')>-1){// Begin stupid IE crap
    var IE=navigator.appVersion;
    IE=IE.slice(IE.indexOf('Trident/')+8);
    IE=IE.slice(0,IE.indexOf(';'));
    IE=Number(IE);
    if(IE>=5&&IE<=7) // IE 9 through IE 11
        document.body.className='ie';
}