treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Test if Mac or PC with JavaScript

Last updated on:

User Agent testing sucks, but sometimes you need it for subtle things. In my case I was using it to adjust what I was showing for keyboard shortcut keys (Command or Control). Nothing super major.

if (navigator.userAgent.indexOf('Mac OS X') != -1) {
  $("body").addClass("mac");
} else {
  $("body").addClass("pc");
}

The statements in there use jQuery to add a body class, but that's not required, you could do whatever.

View Comments

Comments

  1. This may be overkill for your intended use, however it really comes in handy if you need to nudge items or if things aren’t quite right on mobile devices.
    http://rafael.adm.br/css_browser_selector/

Leave a Comment

Use markdown or basic HTML and be nice.