Don't forget navigator.platform

When discussions about JavaScript browser detection pop up, it usually is based around the user-agent string and accessing it via navigator.userAgent. However, there are a number of properties on navigator that are useful. One of the most useful is navigator.platform.

navigator.platform returns a string representing the platform on which the browser is running. The platform is not the operating system per se, but rather the machine architecture. For Windows systems, the value is typically “Win32″ (I’d expect it to be “Win64″ for 64-bit systems, but I don’t have one to test on; it used to be “Win16″ on pre-Windows 95 systems); for Mac systems, it’s either “MacPPC” or “MacIntel” for Unix systems it’s typically “X11″ although Linux systems may have “Linux i686″. This information is useful in determining whether a browser is being used on a computer or a device.

On a hunch, I asked my co-worker Steve to hit a test page using his iPhone. As it turns out, navigator.platform is “iPhone” (I’d expect it to be “iPod” for the iPod Touch). So that’s a very simple way, without user-agent string parsing, to determine that the browser is being used on the iPhone. I suspect (or rather hope) that devices such as the Wii and Playstation also change navigator.platform to something logical. If anyone knows offhand, please let me know.

Another great thing about navigator.platform is that it’s not spoofed, so its value can be relied upon. It can’t tell you explicitly about what browser is being used, but knowing the platform can be useful when cross-platform browser issues arise or for tracking purposes.

Understanding JavaScript Promises E-book Cover

Demystify JavaScript promises with the e-book that explains not just concepts, but also real-world uses of promises.

Download the Free E-book!

The community edition of Understanding JavaScript Promises is a free download that arrives in minutes.