-1

I have a page with form for user registration. I wrapped it with <!--[if IE]> ... <![endif]--> to show it only in IE. And corresponding error is shown for other browsers: <![if !IE]>...<![endif]>.

But this doesn't fully work with IE 10. One can use java-script to determine IE 10, like:

<script type="text/javascript">
    var pattern = /MSIE\s([\d]+)/;
    var ua = navigator.userAgent;
    var matched = ua.match(pattern);
    if (matched) {
        alert("IE 10");
    }
</script>

But how can I bind it with html block I use for IE7+?

Updated

It needs for using the IX509 Windows interface which demands IE usage because it is COM object (I'm not sure, but it seems it uses ActiveX for it), otherwise strange errors will appear on a page when IX509 is used.

Dragon
  • 2,431
  • 11
  • 42
  • 68
  • 2
    Browser detection is almost always a mistake. Generally better to use feature detection instead. What do you need it for? – Spudley Oct 15 '13 at 14:49
  • have a look at this: http://stackoverflow.com/questions/9900311/how-do-i-target-only-internet-explorer-10-for-certain-situations-like-internet-e – pax162 Oct 15 '13 at 14:51
  • @Spudley, this form uses IX509 Windows interface and it demands IE usage due to ActiveX support. So in order to avoid strange errors on a page, this form has to be shown only for IEs. – Dragon Oct 15 '13 at 14:52
  • Fair enough, so just detect whether the ActiveX feature is available. Simples. – Spudley Oct 15 '13 at 14:54
  • See also: http://stackoverflow.com/questions/4313438/how-do-i-detect-if-activex-is-enabled-in-the-browser-of-client – Spudley Oct 15 '13 at 14:54
  • I don't know what IX509 is, but you should also be aware that ActiveX support may not always be available even on IE, particularly more recent versions. Again, feature detection will be more useful for you than browser detection. – Spudley Oct 15 '13 at 14:59
  • @Spudley, I took a look at IX509 - it is COM-object and I'm not sure at the end whether IE uses ActiveX to communicate with it or not (I'm checking it now) – Dragon Oct 15 '13 at 15:06
  • @Dragon - ActiveX really is just the name for COM within the browser, so yes, that's what it will use. – Spudley Oct 15 '13 at 15:09

3 Answers3

3

For ActiveX support, just check that the ActiveXObject exists in JS:

if(typeof window.ActiveXObject != "undefined")

Or more specifically:

if(typeof window.ActiveXObject == "function")

If you're confident that scoping is not an issue in your script and don't want the window., variables are of course automatically fetched from the window object, so you can shorten the code to:

if(typeof ActiveXObject != "undefined")

Though it should only be used with understanding of potential consequences

MDEV
  • 10,730
  • 2
  • 33
  • 49
  • 1
    +1, although I'd suggest using `window.ActiveXObject` rather than just `ActiveXObject`. – Spudley Oct 15 '13 at 14:58
  • @Spudley Yea, I was going to put that, but it shouldn't really be required depending on the implementation - I've added it to the answer anyway to make the OP aware of it. (I don't like scripts with `window.` all over them, so I use it sparingly myself) – MDEV Oct 15 '13 at 15:00
1

Use feature detection, not browser detection.

You can detect whether the browser supports ActiveX with the following simple line of code:

if(typeof window.ActiveXObject != "undefined") { ..... }

Why?

  1. Browser detection is fragile -- newer versions of IE may not be picked up by your detection code, resulting in your site breaking. In fact this is exactly what's happened here. And you'd probably have the same issue all over again with IE11. The above feature detection script will work correctly in all browsers and all versions.

  2. Not all IE installations have ActiveX available. If you detect IE and then assume it supports ActiveX, your site will break badly for those that don't have it. Again, using feature detection instead will avoid this issue.

Spudley
  • 166,037
  • 39
  • 233
  • 307
0

I improved the approach I tried at first.

First of all error page is wrapped with the following <div>:

<div id="not_ie" style="display:none">

IE form is also wrapped with <div>:

<div id="ie" style="display:none">

And the following js makes form or error visible depending on browser type:

<script type="text/javascript">
    var pattern = /MSIE\s([\d]+)/;
    var ua = navigator.userAgent;
    var matched = ua.match(pattern);
    if (matched) {
        var elem = document.getElementById("ie");
        elem.style.display = "inline";
    } else {
        var elem = document.getElementById("not_ie");
        elem.style.display = "inline";
    }
</script>

There's no need in use of conditional comments, determining of ActiveX or version-specific feature of IE.

Dragon
  • 2,431
  • 11
  • 42
  • 68