0

I have a .js file with a instruction that is causing problems in IE. My question is if there is any way of making that instruction to execute in all browsers except in IE.

I guess what I am looking for is something like conditional comments in HTML but for javascript. I know I could have two separate js files for each browser, but I would prefer a cleaner solution.

The solution can be plain js or JQuery.

Cœur
  • 37,241
  • 25
  • 195
  • 267
agente_secreto
  • 7,959
  • 16
  • 57
  • 83
  • possible duplicate of [Javascript IE detection, why not use simple conditional comments?](http://stackoverflow.com/questions/4169160/javascript-ie-detection-why-not-use-simple-conditional-comments) Moreover, why not ask why your function isn't working correctly in IE and what to do about it? – Marcel Korpel Mar 18 '11 at 10:59
  • Its too long too explain, but that function is a workaround for a different problem, and strangely enough, in IE it is not needed. – agente_secreto Mar 18 '11 at 15:04
  • A problem that pops up in all browsers but IE? Sounds very strange to me. Can you describe this problem in a new question? I'm eager to know what it is (and perhaps we can find a solution that fits them all). – Marcel Korpel Mar 18 '11 at 16:56

3 Answers3

2

This is a piece of cake with jQuery:

if ($.browser.msie) {
    // IE specific code.. You could even check for the version $.browser.version
} else {
    // Non-ie specific code
}

Hope this helped.

Kevin
  • 5,626
  • 3
  • 28
  • 41
0

There are many ways, one:

if(!document.all || window.opera)
{
  //this will not be executed in msie
}
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • Oh no, this is browser detection by feature inference; see [Feature detection is not browser detection](http://www.nczonline.net/blog/2009/12/29/feature-detection-is-not-browser-detection/) why this is not a good idea. – Marcel Korpel Mar 18 '11 at 10:57
  • So you recommend to check for e.g. substrings in navigator.userAgent? I think you know that navigator.userAgent can be changed by the user. – Dr.Molle Mar 18 '11 at 11:19
  • No. As stated by Nicholas Zakas in the linked article, "[s]traight feature detection is a best practice, and in almost every case, is exactly what you’ll need. Typically, you just need to know if a feature is implemented before using it." – Marcel Korpel Mar 18 '11 at 11:22
  • But there are not many features that are implemented in all IE's, document.all is one. And there is no feature that is prevented from being implemented in another browser some day. So what would you suggest to identify MSIE? – Dr.Molle Mar 18 '11 at 11:34
  • I fear you don't get the point: just test for the feature you want to use *before* using it and provide a fallback option for browsers that don't support that feature. E.g., see [this code](http://code.google.com/p/oz-js/source/browse/trunk/oz.js#234), which tests for `Array.indexOf` being available and providing a work around if not. – Marcel Korpel Mar 18 '11 at 11:42
  • But the question here doesn't point on a specific feature to test for. – Dr.Molle Mar 18 '11 at 11:45
  • No, and that's why I commented on the question: "why not ask why your function isn't working correctly in IE and what to do about it?", because *that* is the real problem the OP has; browser detection is hardly ever necessary and a bad practice, especially to stop executing a function because is doesn't work properly in a browser. – Marcel Korpel Mar 18 '11 at 11:49
0

I've done something like this once to check for IE, specifically version 6:

var IEVersion = 0;  
if (navigator.appVersion.indexOf('MSIE') != -1)  
    IEVersion=parseFloat(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE') + 5, 3));
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
  • One can't rely on the UA string the browser provides: several browsers (e.g., Opera) can spoof it to make sites work that only work in IE. – Marcel Korpel Mar 18 '11 at 11:03
  • Yeah i know, I just had to get a fast hack working - the above code was supposed to only run in IE, but support older versions :) – Thor Jacobsen Mar 18 '11 at 11:05