There are issues with detection of Opera and IE specifically. Newer versions of Opera throw errors and the generic browser-ie class gets added for all versions of IE.

The IE issues seem to be related to the order of which the User Agent string is read. Opera's User Agent seems to be a bit inconsistent and the usage of strpos doesn't seem to work as intended on $_SERVER['HTTP_USER_AGENT']. Only the elements inside of the () brackets are able to be detected.

The latest version of Opera(10.0) also has the MSIE string in its User Agent. I have moved Opera to the top of my fix to work around this.

The fix below seems to take care of all these issues and adds support for a few more browsers.

// Check what the user's browser is and add it as a body class
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if ($user_agent) {
  if (strstr($user_agent, 'Opera')) {
    $body_classes = 'browser-opera';
  }
  else if (strstr($user_agent, 'MSIE 6.0')) {
    $body_classes = 'browser-ie6';
  }
  else if (strstr($user_agent, 'MSIE 7.0')) {
    $body_classes = 'browser-ie7';
  }
  else if (strstr($user_agent, 'MSIE 8.0')) {
    $body_classes = 'browser-ie8';
  }
  else if (strstr($user_agent, 'MSIE')) {
    $body_classes = 'browser-ie';
  }
  else if (strstr($user_agent, 'Firefox/2')) {
    $body_classes = 'browser-firefox2';
  }
  else if (strstr($user_agent, 'Firefox/3')) {
    $body_classes = 'browser-firefox3';
  }
  else if (strstr($user_agent, 'Firefox')) {
    $body_classes = 'browser-firefox';
  }
  else if (strstr($user_agent, 'Chrome')) {
    $body_classes = 'browser-chrome';
  }
  else if (strstr($user_agent, 'iPhone')) {
    $body_classes = 'browser-iphone';
  }
  else if (strstr($user_agent, 'Version/4')) {
    $body_classes = 'browser-safari4';
  }
  else if (strstr($user_agent, 'Safari/5')) {
    $body_classes = 'browser-safari3';
  }
  else if (strstr($user_agent, 'Safari')) {
    $body_classes = 'browser-safari';
  }
  else if (strstr($user_agent, 'Netscape')) {
    $body_classes = 'browser-netscape';
  }
}

Comments

mcrittenden’s picture

Status: Active » Closed (won't fix)

User agent detection has been removed from beta3 because it doesn't work with Drupal caching enabled. See #413482: Drupal Caching Ruins Browser Body Classes (which is posted against the Basic theme which BlueTrip is built off of).