This question already has an answer here:
-
How do I target only Internet Explorer 10 for certain situations like Internet Explorer-specific CSS or Internet Explorer-specific JavaScript code?
25 answers
How can I make a message box appear on page load if the user is using IE 10?
function ieMessage() {
alert("Hello you are using I.E.10");
}
My webpage is a JSF facelet (XHTML).
The real way to detect this, without conditional comments and without User Agent sniffing is with conditional compilation:
<script type="text/javascript">
var isIE10 = false;
/*@cc_on
if (/^10/.test(@_jscript_version)) {
isIE10 = true;
}
@*/
console.log(isIE10);
</script>
After running this code, you can use following anytime after:
if (isIE10) {
// Using Internet Explorer 10
}
Reference: How can I detect IE10 from JS when browser mode is IE9?
UPDATE:
To avoid minification of comments, 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).
In general, the practice of User Agent sniffing and conditional compilation/comments are best avoided. It is far better to use feature detection, graceful degradation , and progressive enhancement instead. However, for the few edge cases where it is more convenient for the developer to detect the browser version, you can use the following code snippets:
This if
statement will only execute on IE 10
// IF THE BROWSER IS INTERNET EXPLORER 10
if (navigator.appVersion.indexOf("MSIE 10") !== -1)
{
window.alert('This is IE 10');
}
This if
statement will only execute on IE 11
// IF THE BROWSER IS INTERNET EXPLORER 11
var UAString = navigator.userAgent;
if (UAString.indexOf("Trident") !== -1 && UAString.indexOf("rv:11") !== -1)
{
window.alert('This is IE 11');
}
http://jsfiddle.net/Qz97n/
Here's a method for getting the current IE or the IE Version:
function IE(v) {
return RegExp('msie' + (!isNaN(v)?('\\s'+v):''), 'i').test(navigator.userAgent);
}
Here's how you can use it:
if(IE()) alert('Internet Explorer!');
if(IE(10)) alert('Internet Explorer 10!');