473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Simple browser detect

JS noob. I've seen plenty of browser detection scripts but they all seem to
be slightly different and don't really fit my needs. I have various places
where if the browser is IE I'd like to display [this html code] else
[display this]. For example, if a browser is IE I want to use this CSS file
otherwise use a different one and if it's IE make this cell x pixels high
else make it y pixels high. I'm sure this is easy, please excuse my
stupidity.
Steve.
Jul 20 '05 #1
7 10040
Szar wrote:
JS noob. I've seen plenty of browser detection scripts but they all seem to
be slightly different and don't really fit my needs. I have various places
where if the browser is IE I'd like to display [this html code] else
[display this]. For example, if a browser is IE I want to use this CSS file
otherwise use a different one and if it's IE make this cell x pixels high
else make it y pixels high. I'm sure this is easy, please excuse my
stupidity.
Steve.

Browser detection scripts are generally a bad idea. There are multiple
browsers and multiple versions of those browsers running. Many browsers
lie, and many can be set to say they are anything their user wants them
to say. Your detection script, no matter how good, cannot reliably
determine what the browser is.

Almost without exception, when a site fails to operate properly in my
browser, I find that it is because of a browser detection script and
javascript which fails to serve the page because either the site
designer doesn't want to support my browser and deliberately excludes
it, or because the site designer thinks erroneously that my browser
can't render the page, or because the site designer has erroneously
identified my browser as some other browser or browser version.

It is far better to write to standards. When you write to standards and
a page doesn't display properly, the client knows that it is the fault
of his browser -- not because of your code.

Jul 20 '05 #2
Jerry Park <No*****@No.Spam> writes:
Szar wrote:
JS noob. I've seen plenty of browser detection scripts but they all seem to
be slightly different and don't really fit my needs. I have various places
where if the browser is IE I'd like to display [this html code] else
[display this]. For example, if a browser is IE I want to use this CSS file
otherwise use a different one and if it's IE make this cell x pixels high
else make it y pixels high. I'm sure this is easy, please excuse my
stupidity.
Steve.

Browser detection scripts are generally a bad idea. There are multiple
browsers and multiple versions of those browsers running. Many
browsers lie, and many can be set to say they are anything their user
wants them to say. Your detection script, no matter how good, cannot
reliably determine what the browser is.

Almost without exception, when a site fails to operate properly in my
browser, I find that it is because of a browser detection script and
javascript which fails to serve the page because either the site
designer doesn't want to support my browser and deliberately excludes
it, or because the site designer thinks erroneously that my browser
can't render the page, or because the site designer has erroneously
identified my browser as some other browser or browser version.

It is far better to write to standards. When you write to standards
and a page doesn't display properly, the client knows that it is the
fault of his browser -- not because of your code.


You overestimate the common user of IE, the type that think the
"e"-icon opens "the internet". He never heard of other browsers, an
all that matters is that the page works for him.

Otherwise I agree: Write to standards, but allow for a few fall-backs
if the browser doesn't support standards well enough (like IE and CSS2).

The best way to make a fallback, is to use object detection, not
browser detection. That is, instead of:
---
if (browseDetect() == "IE") {
var elem = document.all['foo'];
} else {
var elem = document.getElementById("foo");
}
---
you do:
---
if (document.getElementById) {
var elem = document.getElementById("foo");
} else if (document.all) {
var elem = document.all['foo'];
} else {
// handle error
}
---
Points to notice: Default is the standard method, getElementById. Even
if some new browsers understands document.all, they will use the standard
method first. It is safer that way.
There are browsers other than IE that support document.all, and browsers
that claim to be IE that doesn't support it. Testing for the object you
need directly avoids pitfalls like these.
Be ready to handle the complete failur, just as you are prepared for
Javascript to be completely unavailable.
The safest way to make a specific fallback for IE is to use the
IE-proprietary conditional comments:
<!--[if IE]>
.... all this is only seen by IE, all other browsers think it is
a normal HTML comment
<![end if]-->
However, these only work in IE 5+, and maybe the bug will be fixed in
IE 7, so you should include the version of IE:
<!--[if IE 6]> ... only IE 6 and lower ...

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Lasse Reichstein Nielsen wrote:
Jerry Park <No*****@No.Spam> writes:

Szar wrote:
JS noob. I've seen plenty of browser detection scripts but they all seem to
be slightly different and don't really fit my needs. I have various places
where if the browser is IE I'd like to display [this html code] else
[display this]. For example, if a browser is IE I want to use this CSS file
otherwise use a different one and if it's IE make this cell x pixels high
else make it y pixels high. I'm sure this is easy, please excuse my
stupidity.
Steve.


Browser detection scripts are generally a bad idea. There are multiple
browsers and multiple versions of those browsers running. Many
browsers lie, and many can be set to say they are anything their user
wants them to say. Your detection script, no matter how good, cannot
reliably determine what the browser is.

Almost without exception, when a site fails to operate properly in my
browser, I find that it is because of a browser detection script and
javascript which fails to serve the page because either the site
designer doesn't want to support my browser and deliberately excludes
it, or because the site designer thinks erroneously that my browser
can't render the page, or because the site designer has erroneously
identified my browser as some other browser or browser version.

It is far better to write to standards. When you write to standards
and a page doesn't display properly, the client knows that it is the
fault of his browser -- not because of your code.

You overestimate the common user of IE, the type that think the
"e"-icon opens "the internet". He never heard of other browsers, an
all that matters is that the page works for him.

Otherwise I agree: Write to standards, but allow for a few fall-backs
if the browser doesn't support standards well enough (like IE and CSS2).

The best way to make a fallback, is to use object detection, not
browser detection. That is, instead of:
---
if (browseDetect() == "IE") {
var elem = document.all['foo'];
} else {
var elem = document.getElementById("foo");
}
---
you do:
---
if (document.getElementById) {
var elem = document.getElementById("foo");
} else if (document.all) {
var elem = document.all['foo'];
} else {
// handle error
}
---
Points to notice: Default is the standard method, getElementById. Even
if some new browsers understands document.all, they will use the standard
method first. It is safer that way.
There are browsers other than IE that support document.all, and browsers
that claim to be IE that doesn't support it. Testing for the object you
need directly avoids pitfalls like these.
Be ready to handle the complete failur, just as you are prepared for
Javascript to be completely unavailable.
The safest way to make a specific fallback for IE is to use the
IE-proprietary conditional comments:
<!--[if IE]>
.... all this is only seen by IE, all other browsers think it is
a normal HTML comment
<![end if]-->
However, these only work in IE 5+, and maybe the bug will be fixed in
IE 7, so you should include the version of IE:
<!--[if IE 6]> ... only IE 6 and lower ...

/L

I agree. My website is XHTML1.0 compliant. I had to write some css
specifically to make IE center a division properly. Fortunately, you can
sometimes correct browser errors without messing up the compliant
browsers. However, if I must choose between standards compliant code or
non-compliant code (just to allow a non-compliant browser to display the
page), I'll choose compliant code, for no other reason than that people
should be encouraged to write to the standards.

The code many people write, where there are 47 versions of a page for
multiple browsers, is a nightmare. I can't imagine why anyone would want
to do that.

Jul 20 '05 #4
Please see the page at
http://www.wtv-zone.com/cwdjrsxyz/br...fer_tests.html .Here I
have given fairly detailed browser sniffing results for 6 browsers. At
the bottom of the page are a few comments that may
help.__________________________________________
60000 Year Perpetual Calendar. Official US Holidays are detected and
colored. Try to put in bad data. Validation sinks to a new low. The
calendar is at http://www.wtv-zone.com/cwdjrsxyz/ca...calendar3.html
..Some of the details are given at the page
http://www.wtv-zone.com/cwdjrsxyz/ca...l3_ReadMe.html
.._________________________
Jul 20 '05 #5
Szar wrote:
JS noob. I've seen plenty of browser detection scripts but they all seem to
be slightly different and don't really fit my needs.


You do not need and you do not want such "detection" scripts:

http://pointedears.de.vu/scripts/test/whatami
PointedEars
Jul 20 '05 #6

"Szar" <za*****@hotmail.com> wrote in message
news:KG**************@newssvr23.news.prodigy.com.. .
JS noob. I've seen plenty of browser detection scripts but they all seem to be slightly different and don't really fit my needs. I have various places
where if the browser is IE I'd like to display [this html code] else
[display this]. For example, if a browser is IE I want to use this CSS file otherwise use a different one and if it's IE make this cell x pixels high
else make it y pixels high. I'm sure this is easy, please excuse my
stupidity.
Steve.


In the time it took everyone to write their opinions on why i should not use
brwoser detection couldn't they have just answered my question??? This is
for a page that only myself and one other person will be accessing and I
frankly don't care if it doesn't look good on 100% off all browsers out
there. I care about current IE and Mozilla versions, that's all. The
difference with how IE and others view page margins and a few other things
are throwing off how i'd like images on the page to line up with the
background image. If you want to answer a post then maybe give a little side
note, fine. Otherwise save it.
Jul 20 '05 #7
Szar wrote:
In the time it took everyone to write their opinions on why i should not use
brwoser detection couldn't they have just answered my question???


Your `?' key is b0rken.

They/I could have just answered your question, but this is a discussion
group, no a (paid) support forum. All you get is advice, BTW for free.
PointedEars
Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
1
by: onewebclick | last post by:
Is there a way to detect a browser cache is full using javascript or HTML thorugh a web page and inform the user to clear the cache to improve performance of the website. It looks like google's...
3
by: Denon | last post by:
How to trap browser close event in SERVER side? I read a lot of forum message, it talk about onclose(), onunload() and even onbeforeunload() event. However, all of theses are based on javascript...
6
by: hb | last post by:
Hi, Would you please tell me how to detect if the client's browser is closed? I need such event to trigger a database modification. Thank you hb
7
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I...
13
by: anil.rita | last post by:
When the user chooses an AV file to play, based upon the type of file, I want to use the default installed media player to play it. I am wondering if this is a good way - any alternatives,...
1
by: mark4asp | last post by:
<!-- // How to detect the rendering mode which the browser is currently in (works for IE6). // Ctrl+Shift+s displays indicates whether the browser is in quirks or standards mode. // Detect...
4
by: sid | last post by:
"about:blank" oepns new browser window I am writing a webpage that will run on other machines that I may or may not know about. My page is framed where frame1 controls the content of frame2. ...
1
by: Grzegorz Klimsa | last post by:
Hi ! I have a problem wiht detecting resolution of client web browser I prepare several files of css style for different browsers and different resolutions, (such as : Style_1024x768.css ;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.