Once you created an ActiveX control, you need to embed it in your web page.
This is typically done by creating a new ActiveXObject(“myActiveX”), an object type which is only available in Internet Explorer.
To find out whether this object type is implemented, I try to create it, and check the exception name: Firefox has “ReferenceError”, whereas IE returns “Error”. This information is stored in the variable isActiveXCapable.
Next, we create the ActiveX using its registered class name.
Depending on whether the object can be created and, if not, on the result of the first test, we can distinguish the 3 cases:
- Browser does not support ActiveX
- Browser supports ActiveX, but the requested class is not installed
- Browser supports ActiveX, and an object has been created
<script type="text/javascript"> var myActiveX = null; var isActiveXCapable = false; function InitMyActiveX() { try { new ActiveXObject(""); } catch (e) { // FF has ReferenceError here if (e.name == 'TypeError' || e.name == 'Error') { isActiveXCapable = true; } } try { myActiveX = new ActiveXObject("My.ActiveX"); } catch (e) { myActiveX = null; } if (myActiveX != null) { document.getElementById("myInfo").innerHTML = myActiveX.GetSomeInfo(); } else { document.getElementById("CallMyActiveX").setAttribute("disabled", "disabled"); if (!isActiveXCapable) { document.getElementById("myInfo").innerHTML = "Browser does not support ActiveX"; } else { document.getElementById("myInfo").innerHTML = "MyActiveX is not installed"; } } } function DoSomething() { if (myActiveX != null) { var s = myActiveX.DoSomething(); document.getElementById("myResult").innerHTML = s; } } </script> <div id="myInfo"></div> <input type="button" id="CallMyActiveX" value="Call me" onclick="DoSomething()" /> <div id="myResult"></div>