• 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

    • Report: Trump's T1 Mobile off to a rocky start with messy pre-orders by David Uzondu You might have heard by now that The Trump Organization, spearheaded by President Trump's sons, Donald Trump Jr. and Eric Trump, is launching yet another product to add to the collection. This time, it is a gold smartphone, the T1, and a companion wireless service. The whole operation is being pushed with the usual "America-first" bravado, but it seems they forgot to get the basics right. If you thought you could just hop online and secure your patriotic pocket computer, you are in for a nasty surprise, as the whole process appears to be fundamentally broken. A new report from 404Media details this chaos perfectly, as one of their writers tried to order one of the T1 phones. The goal was simple: pay the $100 preorder deposit and see what this thing is all about when it ships. What happened next was a masterclass in how not to conduct e-commerce. The website crashed, booted him to an error page, and then, for good measure, charged his credit card the wrong amount entirely, taking $64.70. And get this, he received a confirmation email saying his order would ship... despite never once being asked for his shipping address. It is, in his words, the "worst experience I've ever faced buying a consumer electronic product". To add insult to injury, when he tried to log into the new account, the site prompted him to create, and he was immediately met with yet another error page, locking him out. The shoddy experience is not just limited to the checkout. Neowin found a bunch of errors on the official product page. Sure, it boasts a big 6.8-inch Punch Hole AMOLED display with a 120Hz refresh rate and a 50MP main camera, which sounds nice on paper. But then you notice the company completely forgot to mention what processor powers the phone, which is probably a MediaTek. At one point, the page bizarrely listed a "5,000 mAh long life camera," though that has since been fixed. By the way, there's good reason to doubt that this phone will be made in America, despite the press releases insisting it will be. Sourcing all the necessary components without using foreign parts is unbelievably difficult and expensive, something even Apple does not do. The more likely scenario, according to Max Weinbach, is that this is simply a reskin of a much cheaper device, maybe the T-Mobile REVVL 7 Pro 5G, which retails at a fraction of the T1's $499 asking price. The T1 Mobile joins a sprawling collection of other products likely aimed at the same loyal customer. The catalog of gear for this audience already includes the gold "Never Surrender" sneakers, the "God Bless the USA" Bible, "Victory47" perfume, digital trading cards, $TRUMP memecoins, and more. It is still very early days, of course, and while one might forgive some teething issues for a new venture, this initial preorder phase has been exceptionally chaotic. Hopefully, things will become much clearer once there is a firm launch date and a physical product to test. Do you plan to buy the T1 and move to Trump Mobile?
    • Is this release set for the end of this year or for next year?
    • Windows 10 KB5063159 fixes bug that wouldn't let some Microsoft Surface devices boot by Sayan Sen Microsoft released Windows 10 Patch Tuesday updates for the month last week. The one for Windows 10 under KB5060533 / KB5060531 / KB5061010 / KB5060998 introduced a bug that would not let Surface Hub v1 devices start due to a Secure Boot validation issue. As such, Microsoft had paused the update similar to the compatibility blocks or safeguard holds it applies for major feature updates as well. This bug was uncovered after the update went live, as Microsoft later added it to the list of known issues for that update and it also put up a big notice in bold. It wrote: Earlier today, the company released an out-of-band (OOB) update to address the issue. It has been published under KB5063159 and is only being offered to Surface Hub v1 devices instead of the buggy KB5060533 Patch Tuesday one. In the description of the new OOB update, Microsoft writes: You can find the support article for KB5063159 here on Microsoft's website. It is downloaded and installed automatically but users can also manually download it from the Microsoft Update Catalog website.
    • I thought I saw that one, and yeah, it was awhile ago, too..
  • Recent Achievements

    • Week One Done
      korostelev earned a badge
      Week One Done
    • Week One Done
      rozermack875 earned a badge
      Week One Done
    • Week One Done
      oneworldtechnologies earned a badge
      Week One Done
    • Veteran
      matthiew went up a rank
      Veteran
    • Enthusiast
      Motoman26 went up a rank
      Enthusiast
  • Popular Contributors

    1. 1
      +primortal
      683
    2. 2
      ATLien_0
      268
    3. 3
      Michael Scrip
      184
    4. 4
      +FloatingFatMan
      177
    5. 5
      Steven P.
      140
  • Tell a friend

    Love Neowin? Tell a friend!