views:

116

answers:

6

It just occurs to me that for the past decade, all books on HTML/CSS always talk about browser incompatiblities and how to get around that issue by adding more "hacks" to CSS, JS, or HTML. Why is that they never advised that the other way to deal with that (and I believe the best way) is to add code (php, c, whatever) in the first line of a html file, detecting which browser is being used then add appropiate CSS/JS files for that browser?

+2  A: 

It is because of the Server / Client divide. It is not possible to detect a clients settings from code that runs on the server.

There are Conditional Comments in HTML that will allow you to load alternate CSS or Javascript files if you so choose.

A Quickie reference on Conditional Comments

Matthew Vines
You can read the user-agent in the header
Lance Kidwell
This is true, but I have never been able to depend on it. But browser detection is never full proof.
Matthew Vines
Until recently, Opera would pretend to be IE by default to get around websites that had explicitly restricted every browser but IE. Now this isn't so much of a problem, but it does highlight the flaws in depending on user agent strings.
Samir Talwar
I'm not down-voting, but this answer implies that it *is* dependable to detect a client's settings from code that runs client-side. It's not. What if they've got Javascript turned off or a device that doesn't support it?
da5id
I say in my comments that browser detection is never full proof, you can spoof your user agent if you want to. but it is my understanding that conditional comments are pretty solid. Am I incorrect in that understanding?
Matthew Vines
They're definitely solid AFAIK, but if Javascript is turned off or unsupported, how do they help except to know browser/version (which is definitely good for CSS)?
da5id
What's "solid"? Conditional comments are only "solid" for Internet Explorer. Conditional comments do help bring IE inline with most other browsers, and avoid the traditional hacks. But even so, you can have validation concerns.
Fran Corpier
+2  A: 

Because then you have to maintain two sets of css and js files. That's not really any different than adding "hacks" to one, except you have to make changes to two files any time you make a change.

Lance Kidwell
A: 

To further what Matthew Vines said the conditional comments look a bit like this...

<!--[if lte IE 6]><link href="LayoutIE6.css" type="text/css" rel="stylesheet" /><![endif]-->
<!--[if gte IE 7]><link href="LayoutIE7.css" type="text/css" rel="stylesheet" /><![endif]-->
<!--[if !IE]><link href="Layout.css" type="text/css" rel="stylesheet" /><![endif]-->

Hope this helps :)

Chalkey
A: 

It's because (for the most part and 'traditionally' even) CSS hackery was/is implemented by front-end developers, who don't always have access to back-end scripting. If web-development had been driven by back-end developers in the early days I've often thought that things would be very different.

I don't think this is a bad question at all by the way (it was initially voted down for some reason). And, for the record, conditional comments for CSS & a stable JS framework like jQuery are definitely the way to go.

da5id
+4  A: 

In the past you would add hacks in your JavaScript to determine which browser the client was using and run code based on that. It seems like there wasn't a decided on best way to do it so every book had it's own implementation.

You will find in a lot of old books code to determine the browser and then throughout the code you would see things like:

if (NS4) {

} elseif (IE) {

}

But largely that's obsolete now.

Another big thing we have now are frameworks like JQuery which look to abstract some of the few existing incompatibilities that are still there. It seems to me in the past, people would just roll their own style JQuery-esq frameworks so there wasn't a unified one accepted by the larger community.

One of my favorite quotes about user agent strings came with Chrome was released:

"And thus Chrome used WebKit, and pretended to be Safari, and WebKit pretended to be KHTML, and KHTML pretended to be Gecko, and all browsers pretended to be Mozilla, and Chrome called itself Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13, and the user agent string was a complete mess, and near useless, and everyone pretended to be everyone else, and confusion abounded."

It's a good comedy read - http://webaim.org/blog/user-agent-string-history/

Ryan Smith
Good answer and I for one welcome our jQuery overlords, but CSS support is still a mess - some days I think comedy :)
da5id
+1  A: 

If you use purely browser detection, you need to update your list each time a new browser or browser version is released. This causes ridiculous situations like users of a new version being told to upgrade downgrade to an older version.

Browser inconsistencies should be handled by testing for capabilities. As others have mentioned, there are JavaScript frameworks like JQuery which handle this for you. With regards to CSS, use conditional comments to target older versions of Internet Explorer as others have mentioned. Try to avoid using "hacks" because they could be fixed in a newer version without fixing the problem you were trying to work around.

Scott
+1 for mentioning "testing for capabilities", which PHP/Perl, et al cannot do, other than to send down a tiny, fast test (js) file that returns home with the answers (but why do that...when branching can simply be done on the client).
Fran Corpier