Pergunta

I want to target everything except IE, versions 8 and below. Normally, I would use a conditional comment like this:

<!--[if !IE]><!--><p>Do non IE things</p><!--[if !IE]><!-->

However this targets versions 9 and below. Is there any way to only target versions 8 and below, possibly with JavaScript?

Thanks

Foi útil?

Solução

It should be as simple as

<!--[if IE gt 8]><!--> content here <!--<![endif]-->
  • IE 8 and below will respect the condition and not show the content
  • IE 9 will pass the condition (because it is not IE 8) and show the content
  • Everything else (including IE 10 and 11) will ignore the conditional as simply a comment and show the content

JSFiddle demo

(edit: just noticed you said versions 8 and below. Updated answer to reflect that)

Outras dicas

This will not work in IE11, it ignores conditionals. You are better off doing feature detection, rather than the browser sniffing you are doing. Here is the script I use in my SPA to redirect the user to a core site when they are using an obsolete browser.

    <script>

    var ef = "?_escaped_fragment_=";

    if (!('querySelector' in document)
         || !('localStorage' in window)
         || !('addEventListener' in window)
        || !('matchMedia' in window)) {

        if (window.location.href.indexOf("#!") > 0) {
            window.location.href = window.location.href.replace("#!", ef);
        } else {
            if (window.location.href.indexOf(ef) < 0) {
                window.location.href = window.location.href + ef;
            }
        }

    } else {

        if (window.location.href.indexOf(ef) >= 0) {
            window.location.href = window.location.href.replace(ef, "#!");
        }
    }

</script>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top