Question

I know that Modernizr hasn't got a built-in test for ActiveX and detecting ActiveX support for IE gets complicated after IE11 (look at Silverlight version detection code).

Is there any workaround for detecting ActiveX support all versions of IE and other browsers? Also I have to control this server-side too and Request.Browser.ActiveXControls returns false on IE11 (This issue has been reported Microsoft).

Was it helpful?

Solution 2

You can use the following script for client-side and if you save it to element, you can access it via server-side.

function IsActiveXSupported() {
    var isSupported = false;

    if(window.ActiveXObject) {
        return true;
    }

    if("ActiveXObject" in window) {
        return true;
    }

    try {
        var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
        isSupported = true;
    } catch (e) {
        if (e.name === "TypeError" || e.name === "Error") {
            isSupported = true;
        }
    }

    return isSupported;
}

OTHER TIPS

You can test if typeof window.ActiveXObject != 'undefined'.

Note that you MUST check typeof, this is what I got in IE11 (which does not support ActiveX anymore):

>>> window.ActiveXObject
undefined
>>> typeof window.ActiveXObject
"undefined"
>>> typeof window.ActiveXObject === 'undefined'
true
>>> window.ActiveXObject === undefined
false

In IE10 mode and below:

>>> typeof window.ActiveXObject
"function"

So that check will let you find out if you can use ActiveXObject(..) to create a new ActiveX object.

Update on @ogun's code:

function def( o ) { return CS.undefined !== typeof o; }

function IsActiveXSupported() {
    try {
        // Test if it should be supported
        if ( def( window.ActiveXObject ) || ( "ActiveXObject" in window ) ) {
            // Test if it is really supported
            var start = performance.now();
            var xmlDom = new ActiveXObject( "Microsoft.XMLDOM" );
            var elapsed = performance.now() - start;
            return true;
        }
    } catch ( e ) {
        // When active X controls are possible, but disabled, an exception will be thrown.
    }
    return false;
}

Reasons for the update: When ActiveX is possible, but disabled, an exception will be thrown. The original code would indicate this as 'enabled'. Don't call this function repeatedly, as it is not very fast. Tested on IE11, current Firefox and Chrome. (You can remove the performance measuring.)

If all else fails, you can fall back to a try-catch block:

try {
    var x = new ActiveXObject("...");
    // this particular ActiveX Control is supported
}
catch (error) {
    // it's not supported
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top