Post Archive

› May 28, 2003

Simple Win vs. Mac IE Javascript detection

  • Reported by pixy

Following test may be used for very simple detection of Windows, or MacOS IE. It's faster and easier than parsing navigator.userAgent. Just test two methods: document.all (fails everywhere but in any IE), and window.print (fails in MacIE). E.g. WinIE can be simply detected as if (document.all && window.print).

Comments

1. May 28, 2003 06:34 PM

Quote this comment

Simon Willison Posted…

Of course, before you do this you should think about whether or not testing browsers is a good idea in the first place. While this technique works at the moment, it's possible another browser may come out in the future that passes these tests (or fails them) but doesn't replicate the browser specific feature you are working with. It's best to use object detection to look for the exact functionality you intend to use - for example, you might want to check that document.getElementByID exists before using it in a script.

2. May 28, 2003 07:42 PM

Quote this comment

pixy Posted…

Of course, Simon. I allways use the document.getElementById test, if I need it (or document.styleSheets test, if neccessary, or so...). This is just an idea for everyone who knows very well what's he/she doing... ;) However, I often need to differentiate between Win and Mac IE in my scripts - this seems to be very fast and simple way. What do we now - maybe next version of MacIE will support window.print as well as filters, or document.selection as WinIE does... Meanwhile, I prefer this way than something like "if (navigator.userAgent.indefOf('Mac')>-1" etc... It's a little bit dangerous - what if an (exotic) browser uses its userAgent string containing something like "Machiavelli"... Then I have to use more checks for "Mac", "MacOS", "MacOS X", "MacOSX", "Mac_PowerPC" etc. Lots of work, too long scripts. While I'm keeping this script in my mind, and I'll check it if next version of MacIE comes out, everything's OK...

3. May 28, 2003 07:44 PM

Quote this comment

pixy Posted…

I'm sorry: "What do we *K*now..."

4. May 29, 2003 05:55 PM

Quote this comment

liorean Posted…

Hmm, there are other much better methods of doing it for the moment. Check for window.clientInformation instead of window.navigator first. It's for the moment ie only. Then, check clientInformation.platform for /mac/i. Of course, it's better to feature check for each given situation, using platform detection only where feature detection isn't enough. DOM, iem and iew sometimes differ from eachother (yep, all three might be different - or at least is in one case), and then you better detect either how it works (if you can) or platform. Also, there are some things that iem doesn't have, such as addEvent, that you can check for. It's also ie only.

5. March 16, 2006 04:09 PM

Quote this comment

markymark Posted…

Hmm. Doesn't look like there is going to be another version of IEMac