• 0

Script that checks browser and load CSS


Question

I searched google, I searched hotscripts. Im looking for a JavaScript or even a php script that (preferably php but JS works fine also) that checks the browsers user-agent for a Mozilla borwser (Netscape, Firefox Camino, Mozilla) and a IE browser then loads the appropriate Stylesheet styleIE.css and styleMZ.css. Possible if so can you post the script to do it with?

Thanks.

7 answers to this question

Recommended Posts

  • 0

haha, I used it for my website the code as follow:

<?

if (preg_match("/msie.[4|5|6]/i",$_SERVER["HTTP_USER_AGENT"])) {

?>

<link href="css/style.css" rel="stylesheet" type="text/css">

<?

}

else {

?>

<link href="css/style1.css" rel="stylesheet" type="text/css">

<?

}

?>

  • 0
  Huezo said:
Now that the topic has been brought up, how could I detect if a user is using a Mac or not?

I would like to code "Special" things on my sites for Mac users, just as an incentive. :happy:

The problem with browser detection is that sometimes they lie.

Safari can be told to advertise as pretty much any browser, shiira sends it's own user agent even though it's using webkit and should say it's khtml. Opera on all platforms (IIRC) claims to be IE6/windows.

You can write any CSS rules you like and let browsers will skip the ones they don't understand. Example: I'm using both color and drop shadow on my text hyperlinks. IE6/Firefox will have the links change color on mouse over but only safari will have a drop shadow (which gets bigger on mouse over). I also have "footers" on my blockquotes that contain the source information. Safari and Firefox both understand the :after pseudo class and will display that correctly, IE6 doesn't and just skips over the rule.

OpenBSD's pf firewall can detect operating systems of connecting computer and apply rules accordingly so you could have it detect incomming connections and foward it to another 'site' (or vitual host, etc). You could also use a javascript detection (macintosh systems will typically have 'PPC' in the user-agent string) script and feed people custom data.

The best method IMO is to just write what you want and let anybody who can display it do so.

  • 0

Don't use userAgent, it can't be relied upon.

If you have to use the navigator object, then use navigator.appVersion (AFAIK this can't be set by user):

IE has MSIE x.y (where x is major version, y is minor version) in it

Mozilla has Gecko in it.

Apple browsers tend to have Mac in it.

But a far better way is to use object detection and load the appropriate stylesheet dynamically.

if(typeof HTMLElement!='undefined'){ // most likely Gecko

document.write('<linktylesheet" type="text/css" src="styleMZ.css" />');

}

else if(document.all){

document.write('<linktylesheet" type="text/css" src="styleIE.css" />');

}

This also means that browsers that don't have either those objects won't load a stylesheet at all...

The best way is to just write one stylesheet with hacks to apply browser specific styles and call in it via the <link/> tag:

write the styles to work in a decent browser (like Mozilla)

for each wrong one in IE:

* html (class|id|tag) {

}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.