1

How can I remove a script if the browser is IE10?

I tried to do it like this:

<!--[if !IE]-->
    <script type="text/javascript" src="js/script.js"></script>
<!--[endif]-->

But since IE10 doesn't support conditional comments anymore, it still loads the script.

Any ideas?

iker30
  • 11
  • 1
  • 2
    This should help: [link](http://stackoverflow.com/questions/16135814/check-for-ie-10) – tszarzynski Sep 27 '13 at 10:49
  • possible duplicate of [How do I target only Internet Explorer 10 for certain situations like Internet Explorer-specific CSS or Internet Explorer-specific JavaScript code?](http://stackoverflow.com/questions/9900311/how-do-i-target-only-internet-explorer-10-for-certain-situations-like-internet-e) – Pavlo Sep 27 '13 at 10:50
  • And **[this](http://stackoverflow.com/a/9900331/590525)** should help as well. – gnclmorais Sep 27 '13 at 10:51
  • Yes I know that, but how can I remove the script then. I guess I'd knew how to add it, but how to remove it?? – iker30 Sep 27 '13 at 10:55
  • 2
    First, ask yourself why you need to remove the script from IE. Conditional comments were removed from IE10 because stuff like this should not be necessary any more, as the browser is more standards compliant than earlier versions. If you tell us why you're doing this and why you need to keep doing it, we may be able to find a better solution for you. – Spudley Sep 27 '13 at 11:35

2 Answers2

2

You can achieve this by appending this script tag in head if the browser is not IE10 by using this code in javascript:

    **//detect IE10**
    var isIE10 = false;
    /*@cc_on
     if (/^10/.test(@_jscript_version)) {
     isIE10 = true;
     }
     @*/
    if(!isIE10){
       **//append script tag to head if not IE10**
        var script = document.createElement('script');
        script.type = '"text/javascript"';
        script.src = 'js/script.js';
        document.head.appendChild(script);
    }

I hope this may help you. Thanks.

Pushpak
  • 148
  • 5
1

Use feature detection in your code, not browser sniffing.

Browser sniffing is unreliable and subject to random changes. Detecting user agent capabilities is by far a better approach to coding.

itmitica
  • 483
  • 2
  • 10
  • 1
    Good advice, but doesn't answer the question. – Pavlo Sep 27 '13 at 13:09
  • The answer is dependent on what the OP wants done. Usually, and unfortunately for the programmer, this spells a different approach than the current one. – itmitica Sep 27 '13 at 14:31