Tweaking Various Body Classes for Cross-Browser Compatibility in Drupal

Have you ever noticed that some CSS implementation in the Mozilla Firefox browser displays differently than in today’s other leading browsers, including Internet Explorer, Google Chrome, Opera and Safari? If you have, then this blog is for you! Often times, only web developers notice these differences, but if you are a keen observer of how a website looks, you will definitely spot the differences. This article discusses ways to implement the cross-browser compatibility check in Drupal sites. 

You will use the lines of code given below.
In a text editor (I've used Notepad++), open your template.php file located in the /sites/all/themes/[name_of_your_theme]/ folder. If you don't have a template.php file, create it. 
Copy the lines of code of two PHP functions, displayed below, into the template.php file. 
Don't forget to change the [name_of_the default_drupal_theme] with the name of your default Drupal theme.
<?php
function [name_of_the_default_drupal_theme]_preprocess_page(&$vars) {
  $vars['body_classes'] .= ' ';
  $vars['body_classes'] .= browser_detect();

  if(trim(strtolower($vars['title'])) == 'english'){
    $vars['body_classes'] .= ' ';
    $vars['body_classes'] .= 'english-page-ltr-style';
  }
}

function browser_detect() {
  $browsers = "mozilla msie gecko firefox ";
  $browsers.= "konqueror safari netscape navigator ";
  $browsers.= "opera mosaic lynx amaya omniweb chrome";
  $browsers = split(" ", $browsers);

  $nua = strToLower( $_SERVER['HTTP_USER_AGENT']);
  $l = strlen($nua);

  for ($i=0; $i<count($browsers); $i++){
    $browser = $browsers[$i];
    $n = stristr($nua, $browser);
    if(strlen($n)>0){
      $GLOBALS["ver"] = "";
      $GLOBALS["nav"] = $browser;
      $j=strpos($nua, $GLOBALS["nav"])+$n+strlen($GLOBALS["nav"])+1;
      for (; $j<=$l; $j++){
        $s = substr ($nua, $j, 1);
        if(is_numeric($GLOBALS["ver"].$s) )
        $GLOBALS["ver"] .= $s;
        else
        break;
      }
    }
  }

  $my_browser = ($GLOBALS["nav"] . " " . $GLOBALS["ver"]);

  switch ($my_browser) {
  case "msie 6.0":
    $my_browser = "ie ie6 ie8-earlier";
    break;
  case "msie 7.0":
    $my_browser = "ie ie7 ie8-earlier";
    break;
  case "msie 8.0":
    $my_browser = "ie ie8 ie8-earlier";
    break;
  case "msie 9.0":
    $my_browser = "ie ie9";
    break;
  }

  return $my_browser;
}
Save the file. 
Open any browser and go to your website. 
After the site loads, right click anywhere in the page and view the page source. 
Look for the "class" attribute inside the <body> tag.
You will then see the name of the browser inside the "class" attribute. Use this name to style your website. So for example, in the Firefox browser, you will see the word 'firefox' inside the "class" attribute. 
To modify certain styles in your website for this particular browser (Firefox), inside your CSS file, precede all of your styles that need modification with this CSS code: body.firefox.
 
And that's it! You can now modify your Drupal CSS depending on the need of the browser. You can also make use of tools available online to test your website’s cross-browser compatibility by visiting this

 

Comments

Post new comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.