0

I have been using two types of conditional statements of my own creation on my website. I use these statements to detect browsers and to output different stylesheet. (I know there are better and more complex ways of doing this, but this is beside the point.) As you can see, MSIE 8, 9, 10, etc. use the same stylesheet, but I am not quite sure as to how to group the statements:

<?php
if (strStr(getEnv('HTTP_USER_AGENT'), 'Opera')) 
{ 
  echo '<link rel="stylesheet" href="http://www.example.com/nn_styles.css" type="text/css" media="screen">' . "\n" . '<style type="text/css">@import url("http://www.example.com/op11_styles.css");</style>' . "\n"; 
} 
else if (strStr(getEnv('HTTP_USER_AGENT'), 'Gecko')) 
{ 
  echo '<link rel="stylesheet" href="http://www.example.com/nn_styles.css" type="text/css" media="screen">' . "\n" . '<style type="text/css">@import url("http://www.example.com/ch_styles.css");</style>' . "\n"; 
}
else if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 6.0')) 
{ 
  echo '<link rel="stylesheet" href="http://www.example.com/ie_styles.css" type="text/css" media="screen">' . "\n" . '<style type="text/css">@import url("http://www.example.com/ie6_styles.css");</style>' . "\n"; 
}
else if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 7.0')) 
{ 
  echo '<link rel="stylesheet" href="http://www.example.com/ie7_styles.css" type="text/css" media="screen">' . "\n" . '<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>' . "\n"; 
}
else if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 8.0')) 
{ 
  echo '<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>' . "\n" . '<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>' . "\n"; 
}
else if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 9.0')) 
{ 
  echo '<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>' . "\n" . '<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>' . "\n"; 
}
else if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 10.0')) 
{ 
  echo '<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>' . "\n" . '<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>' . "\n"; 
}
?>

This is the JS version of the same script:

var MSIE10=navigator.userAgent.indexOf("MSIE 10.0");
var MSIE9=navigator.userAgent.indexOf("MSIE 9.0");
var MSIE8=navigator.userAgent.indexOf("MSIE 8.0");
var MSIE7=navigator.userAgent.indexOf("MSIE 7.0");
var MSIE6=navigator.userAgent.indexOf("MSIE 6.0");
var NETS=navigator.userAgent.indexOf("Gecko");
var OPER=navigator.userAgent.indexOf("Opera");
if(OPER>-1) {
document.write('<link rel="stylesheet" href="http://www.example.com/op_styles.css" type="text/css">');
document.write('<style type="text/css">@import url("http://www.example.com/op11_styles.css");</style>');
}
else if(MSIE6>-1){
document.write('<link rel="stylesheet" href="http://www.example.com/ie_styles.css" type="text/css">');
document.write('<style type="text/css">@import url("http://www.example.com/ie6_styles.css");</style>');
}
else if(MSIE7>-1){
document.write('<link rel="stylesheet" href="http://www.example.com/ie7_styles.css" type="text/css">');
document.write('<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>');
}
else if(MSIE8>-1){
document.write('<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>');
document.write('<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>');
}
else if(MSIE9>-1){
document.write('<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>');
document.write('<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>');
}
else if(MSIE10>-1){
document.write('<style type="text/css">@import url("http://www.example.com/ie7_styles.css");</style>');
document.write('<style type="text/css">@import url("http://www.example.com/ie7_9_styles.css");</style>');
}
else {
document.write('<link rel="stylesheet" href="http://www.example.com/nn_styles.css" type="text/css">');
document.write('<style type="text/css">@import url("http://www.example.com/ch_styles.css");</style>');
}
Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89

4 Answers4

1

Sidestepping the code organization question, consider using the built-in browser detection functions on the server. Code to parse user agent string?

As for the client, it's usually a good idea to avoid doing anything browser-specific in JS, and instead use a framework (like jQuery) that hides these differences from you. In this case it seems like you can conditionally link to the correct style sheet based on the server's browser detection.

Community
  • 1
  • 1
Stephen Schwink
  • 512
  • 4
  • 7
0

Try this

if (strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 7.0') || strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 8.0') || strStr(getEnv('HTTP_USER_AGENT'), 'MSIE 9.0')) 
Sudip Pal
  • 2,041
  • 1
  • 13
  • 16
  • Sounds awesome! However, I know very little about JS. Can you briefly explain how these double-pipe things work? Is it like "or"? :) –  Mar 04 '13 at 07:18
  • Try to search conditional operator like '&&' and '||' in web, you will get better idea. – Sudip Pal Mar 04 '13 at 07:20
0

Better use html conditional statements:

<!--[if IE 6]>
<link rel="stylesheet" href="http://www.example.com/ie_styles.css" type="text/css">
<style type="text/css">@import url("http://www.example.com/ie6_styles.css");</style>
<![endif]-->

Replace the ifs with switch

switch (strtolower(getEnv('HTTP_USER_AGENT'))) {
   case 'opera':
   ...
   break;
}

And for the JS part you can use switch (true) and check various conditions as case statements

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
0

You may need to review your HTML markup if you really need a stylesheet for every browser. But to answer your question this is how I would do it. There may be some errors, I didn't test it.

PHP

$user_agents = array(
    'Opera' => 'nn',
    'Gecko' => 'nn',
    'MSIE 6.0' => 'ie'
);

$style_sheet = 'ie7'; // default
foreach ($user_agents as $agent => $sheet) {
    if (strStr(getEnv('HTTP_USER_AGENT'), $agent)) {
        $style_sheet = $sheet;
        break;
    }
}

echo '<link rel="stylesheet" href="http://www.example.com/'.$style_sheet.'_style.css" type="text/css" media="screen">';

Javascript

You should have a consistant naming convention for your style sheets to make it easier.

var user_agents = ['MSIE 9.0', 'MSIE 6.0', 'MSIE', 'Gecko', 'Opera'],
    style_sheets = ['ie7_9', 'ie', 'ie7', 'nn', 'op'], // indexs correspond with user_agents array
    inserted = false;

user_agents.forEach(function(item, index) {
    if (!inserted && navigator.userAgent.indexOf(item) > -1) {
        document.write('<link rel="stylesheet" href="http://www.example.com/'+style_sheets[index]+'_styles.css" type="text/css">');
        document.write('<style type="text/css">@import url("http://www.example.com/'+style_sheets[index]+'_styles.css");</style>');
        inserted = true;
    }
});
Seain Malkin
  • 2,273
  • 19
  • 20