Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for IE 10 [duplicate]

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).

like image 880
user2292674 Avatar asked Apr 21 '13 20:04

user2292674


People also ask

How do I check my IE browser?

Press the Alt key (next to the Spacebar) on the keyboard to open a menu bar. Click Help and select About Internet Explorer. The IE version is displayed in the pop-up window.

What is Internet Explorer version 20H2?

Internet Explorer 11 (Version 20H2)

How can you tell if someone is using Internet Explorer?

To detect whether the current browser is Internet Explorer, you can make use of the navigator. userAgent property. The userAgent property returns the value of the user-agent header sent by the browser to the server. It contains information about the name, version, and platform of the browser.

How do I see older versions of Internet Explorer?

IETester IETester is a software that is free to download. It allows you to run your web pages across multiple versions of Internet Explorer, even as low as IE5. 5 It also allows the user to check how the website performs on different browser versions in Windows 10, 7, Vista and XP.


3 Answers

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).

like image 64
Ian Avatar answered Oct 20 '22 05:10

Ian


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/

like image 34
Alex W Avatar answered Oct 20 '22 04:10

Alex W


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!');
like image 20
Daryl Ginn Avatar answered Oct 20 '22 05:10

Daryl Ginn