5

i want a javascript code to disable the script i made if the user is browsing using Internet explorer 8.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
johnathan
  • 63
  • 1
  • 1
  • 3
  • 4
    Generally speaking "If using IE8" is a very poor decision point. What is the problem with 8 that you don't have with 7 or 9? Why can't you fix it? Why can't you test for the problem instead of the browser? – Quentin Jul 20 '11 at 14:43
  • actually i want to disable the script for users using 8 and above. I have not problem with my script on IE7. If you can help me to disable it on all IE , i will really appreciate it. Thank you – johnathan Jul 20 '11 at 14:45
  • why disable? you should write javascript code that works on all major browsers and should work on ie8 / 9 tried it with jquery? –  Jul 20 '11 at 15:05

3 Answers3

13

Try this.

For disabling script for IE 8

<!--[if !(IE 8)]><!-->
    <script src="path/to/external/script"></script>
    <script>
        // your inline script goes here
    </script>
<!--<![endif]-->

For disabling script for IE 8 and above

<!--[if !(gte IE 8)]><!-->
    <script src="path/to/external/script"></script>
    <script>
        // your inline script goes here
    </script>
<!--<![endif]-->

Read bobince's answer: Conditional comment for 'Except IE8'?

Community
  • 1
  • 1
naveen
  • 53,448
  • 46
  • 161
  • 251
  • 1
    in the "path/to/external/script" i will replace it with the external javascript file i want to disable right? What i must add to " // your inline script goes here" ?? I want just to disable the script on IE8 – johnathan Jul 20 '11 at 15:01
  • This disable the call in other browsers than IE8+, so it won't work for chrome nor firefox:( – Wissem Aug 21 '14 at 14:45
2

One way to do this would be to use another script, enclosed in IE-specific conditional comments, to deactivate the first one. Of course, this means you need to have some way to deactivate the 1st one, but that's not an IE-specific issue.

Something like this:

<!--[if (gte IE 8) ]><script>deactivate1stScript();</script><![endif]-->

By using the conditional comments, you are letting IE do the work, and no other browser will have to process any extra JavaScript because only IE will pay attention to the code in the comments (and IE8 specifically)

However, I do agree with @Quentin that targeting specific browsers is usually not the best idea. If there is certain functionality that your script needs, such as Canvas or SVG support, using a feature-detection library such as Modernizr is a much better way to deal with this.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
1

See very straightforward solution here How to detect IE8 using JavaScript

The main idea is to run javascript functuion which return version of a current browser and than construct your script usng conditional statements like if, switch

sll
  • 61,540
  • 22
  • 104
  • 156
  • Just a link === Not an answer – Wesley Murch Jul 20 '11 at 14:46
  • 1
    @Wesley Murch: Hey, I wont copy paste content across a intrawebs, the hyperlink specially designed for this purposes. Sometimes hyperlink to a good solution better than posting ugly code or copy paste thm without whole article notes which could be vital, so I'm not sure whether you're right when downvoting my answer – sll Jul 20 '11 at 14:47
  • 1
    See here: http://en.wikipedia.org/wiki/Link_rot and here [Are answers that just contain links elsewhere really "good answers"?](http://meta.stackexchange.com/q/8231) and here [Why is linking bad?](http://meta.stackexchange.com/q/7515) Adding at least some context to your answer is perfectly fine, especially if you attribute the source as you are doing. – Wesley Murch Jul 20 '11 at 14:51
  • Thanks for the links, I'll read and than reconsider my point of view – sll Jul 20 '11 at 14:52
  • The only problem with this solution is that all browsers are penalized because they all will have to run that javascript to determine if they are IE8. People using well behaved browsers (I'm assuming IE8 is not well behaved which is why the other script needs to be deactivated) should not be penalized for IE8 behaving poorly. – cdeszaq Jul 20 '11 at 14:55
  • @cdeszaq: agree with you but how you can do it without any runtime checks? Anyway you have to execute some logic for each page load regardless of a current browser version – sll Jul 20 '11 at 14:59
  • @sll - If you use a conditional comment, all non-IE browsers will ignore it completely and will have only the overhead of parsing an HTML comment. IE browsers will check to see if they need to process it based on their version, and if not, will just skip over it, so it is a very small overhead. All browsers, however, _will_ get hit with the increased page size, but there's not really a good client-side way around that. Other than that, only the indicated browsers will have the overhead of running the JavaScript, which can be substantial. – cdeszaq Jul 20 '11 at 15:02
  • @cdeszaq: Cool stuff, I'll vote for your solution, really nice. BTW, javascript also increaases an amount of bytes which client need to load along with page, so don't worry about this cons of solution you've proposed – sll Jul 20 '11 at 15:06
  • @Wesley Murch: makes sense, I've added more details to my answer – sll Jul 20 '11 at 15:10