• 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.
  • Posts

    • RIP Hotlips..... IMHO, her best scenes were the few where she dared to let her REAL feminine side show through from underneath all that crappy, worn-torn soldier facade that she had to keep up with...she instantly lit up every room or situation where she was featured !
    • Helium Converter 3.3.69.0 by Razvan Serea Helium Converter is a free Windows utility for converting audio files between formats such as MP3, FLAC, AAC, WMA, OGG, and WAV. It supports batch conversion, preserves or updates tag information, and offers features like volume normalization. With a simple interface, it's ideal for users who need to convert large music libraries quickly and efficiently while retaining metadata. Helium Converter key features: Supports file formats: MP3, MP4, FLAC, AAC, M4A, WMA, WAV, OGG, OPUS, APE.... Batch conversion for large music libraries Preserves and edits metadata (ID3, Vorbis Comments, etc.) Volume normalization to equalize loudness Album art extraction and embedding Drag-and-drop interface for quick file selection Adjustable encoding parameters (bitrate, sample rate, channels) Uses internal codecs for consistent performance Supports CUE sheets for split track conversion File renaming based on tags during export Unicode support for international file and tag names Logging of conversion processes for troubleshooting Multi-core CPU support for faster conversions Download: Helium Converter 3.3.69.0 | 25.1 MB (Freeware) Links: Helium Converter Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Since it's been quite a while since the last episode aired, would it be fair (or cruel) to refer to Peggy as a MILF, or even a SMILF ?  HAHAHAHAHAHA
    • Their computers are not gonna stop working in October
    • Crowdstrike the same service provider that caused millions of in damages? I hate disliking a company for a singular failure but they really screwed up.
  • Recent Achievements

    • One Year In
      WaynesWorld earned a badge
      One Year In
    • First Post
      chriskinney317 earned a badge
      First Post
    • Week One Done
      Nullun earned a badge
      Week One Done
    • First Post
      sultangris earned a badge
      First Post
    • Reacting Well
      sultangris earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      173
    2. 2
      ATLien_0
      125
    3. 3
      snowy owl
      122
    4. 4
      Xenon
      116
    5. 5
      +Edouard
      93
  • Tell a friend

    Love Neowin? Tell a friend!