-1

There might be a lot of question and answers but I couldn't find it really the best, fastest and easiest way to detect the internet explorer version of lower than 10.

So, how can I detect internet explorer less than 10?

Navin Rauniyar
  • 10,127
  • 14
  • 45
  • 68

2 Answers2

0

There is no solution as fast, and as short as the solution listed below.

var IElt10=0/*@cc_on@if(@_jscript_version<10)1@end@*/;

The solution above will set the value of the variable IElt10 to 0 (which evaluates as false in an if-statement) if the browser is greater than 10, and 1 (which evaluates as true in an if-statement) if the browser is less than 10. This will allow you use the above code to do something such as:

var IElt10=0/*@cc_on@if(@_jscript_version<10)1@end@*/;
if (IElt10){
    alert("You are using a version of internet explorer less than 10");
} else {
    alert("You're not using a version of internet explorer less than 10");
}

It works based on conditional comments. Because conditional comments are still comments, most (if not all) minifiers won't detect that a conditional comment is important and discard it with the rest of the comments. An easy way to overcome this is by simply placing the value of IElt10 in something like alert as shown below:

Source Code:

var IElt10=alert(0/*@cc_on@if(@_jscript_version<10)1@end@*/);
if (IElt10){
    alert("You are using a version of internet explorer less than 10");
} else {
    alert("You're not using a version of internet explorer less than 10");
}

Then, you can minify your code. And, in the minified code, find the alert, and replace the whole alert statement (i.e. the entire alert(0)) with the original expression. For example:

Minified Before:

alert(0)?alert("You are using a version of internet explorer less than 10"):alert("You're not using a version of internet explorer less than 10")

Now, replace that alert(0) with 0/@cc_on@if(@_jscript_version<10)1@end@/ as shown below.

Minified After:

0/*@cc_on@if(@_jscript_version<10)1@end@*/?alert("You are using a version of internet explorer less than 10"):alert("You're not using a version of internet explorer less than 10")

And, wala: you outsmarted the minifier, so now you have working, minified code.

Jack G
  • 4,553
  • 2
  • 41
  • 50
-1

You could test for the user agent string in the browser, but these days that is discouraged in opposition to detecting specific features that your web app requires. Check out frameworks like Modernizr which make that easier for you. If you really want to go for detecting IE <10, there is "MSIE" in the UA string followed by a version, e.g. "MSIE 9.0"

ystan-
  • 1,474
  • 1
  • 19
  • 43