Browser Detection Script Example

The script below demonstrates browser detection and style sheet selection by a CGI script. The purpose of the script is to deliver a style sheet that is optimized for a specific browser type to make the HTML output as similar as possible across platforms. The MASTER-STYLE-SHEET would contain the bulk of formatting instructions. This style sheet would be included in every output page. The browser-specific stylesheets would include corrective instructions needed to normalize the output for the respective browser type. These instructions are printed last so that they can overwrite the instructions of the master style sheet.

This script prints only one resume. However, the same principle of selecting stylesheets based on browser detection can also be applied to resume selection based on form data input.

#!/usr/local/bin/perl

# the master style sheet
$style_master = "[MASTER-STYLE-SHEET]";

# browser-specific stylesheet corrections;
@style_sheets = ('', 
                 "[MOZILLA_STYLESHEET]", 
                 "[INTERNET_EXPLORER_STYLESHEET]", 
                 "[SAFARI_STYLESHEET]");
$ss_index     = undef;
$cv           = "[RESUME_SOURCE]";
$unknown      = "unrecognized";

# ------------- start program ------------- #

$ua           = $ENV{'HTTP_USER_AGENT'};
@returned     = &parse_browser($ua);
$browser      = shift(@returned); 
$ss_index     = shift(@returned);

&print_HTTP_header;
&print_head($style_master, $style_sheets[$ss_index]);
&print_body;
&print_footer;

# -------------- subroutines -------------- #

# takes the user agent as an argument and returns a text
# string with the user agent and an integer value that
# becomes the index into @style_sheets array
sub parse_browser($ua)
{
    my $ua = shift;
    my $browser_type;
    my $temp;
    my $index;

    if($ua =~ m/(Safari.+)\b/)
    {
      $browser_type = $1;
      $browser_type =~ s/\// /;
      $ua =~ m/\([ \w]+;[ \w]+; ([^;]+);/;
      $temp = $1;
      $temp =~ s/ /_/g;
      $browser_type = $browser_type . ' ' . $temp;
      $index = 3;
    }
    elsif ($ua =~ m/(MSIE[ .0-9]+)/)
    {
      $browser_type = $1;
      $ua =~ m/\(.+;.+;(.+\b)/;
      $browser_type = $browser_type . $1;
      $index = 2;
    }
    elsif($ua =~ m/(Netscape.*)\b/)
    {
      $browser_type = $1;
      $browser_type =~ s/\// /;
      $ua =~ m/\([ \w]+;[ \w]+; ([^;]+);/;
      $temp = $1;
      $temp =~ s/ /_/g;
      $browser_type = $browser_type . ' ' . $temp;
      $index = 1;
    }
    elsif($ua =~ m/^(Mozilla).+rv:([0-9.]{3,})\).+Firefox\/(.*)/)
    {
      $browser_type = $1 . ' ' . $2 . " (Firefox " . $3 . ")";
      $ua =~ m/\([ \w]+;[ \w]+; ([^;]+);/;
      $temp = $1;
      $temp =~ s/\s/_/g;
      $browser_type = $browser_type . ' ' . $temp;
      $index =1;
    }
    elsif($ua =~ m/^(Mozilla).+rv:([0-9.]{3,})\)/)
    {
      $browser_type = $1 . ' ' . $2;
      $ua =~ m/\([ \w]+;[ \w]+; ([^;]+);/;
      $temp = $1;
      $temp =~ s/\s/_/g;
      $browser_type = $browser_type . ' ' . $temp;
      $index =1;
    }
    else
    {
      $browser_type = $unknown;
      $index = 0;
    }
    return $browser_type, $index;
}

sub print_HTTP_header()
{
    print "Content-type: text/html\n\n";
}

sub print_head($master_sheet, $browser_sheet)
{
    print <<END;
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
     \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html lang=\"en\">
<head><title>My Resume</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />
<style type=\"text/css\">";
END

    foreach $sheet (@_)
    {
      next if ($sheet eq '');
      open(STYLE, "<$sheet");
      while (<STYLE>)
      {
        print $_;
      }
      close(STYLE);
    }
    print "\n</style\>\n</head>\n<body>\n";
}

sub print_body
{
   open(CV, "<$cv");
   while (<CV>)
   {
      print $_;
   }
   close(CV);
}

sub print_footer
{
    if ($browser ne $unknown)
    {
      $browser =~ s/_/ /g;
      print "<div class=\'fl_right\'><p class=\"bold\">0ptimized for: <span class=\"plain\">$browser</span></p></div>";
    }
    print "\n</body>\n</html>\n";
}