473,598 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10066
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.Spa m> 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.getEle mentById("foo") ;
}
---
you do:
---
if (document.getEl ementById) {
var elem = document.getEle mentById("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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Lasse Reichstein Nielsen wrote:
Jerry Park <No*****@No.Spa m> 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.getEle mentById("foo") ;
}
---
you do:
---
if (document.getEl ementById) {
var elem = document.getEle mentById("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*****@hotmai l.com> wrote in message
news:KG******** ******@newssvr2 3.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
2877
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 loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after the preload has been completed.
1
3183
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 gmail use to detect and warn "Gmail says my browser's cache is full" http://gmail.google.com/support/bin/answer.py?answer=14106 then there should be a way to do it if google can do it. Any thoughts ?
3
11214
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 written on the client browser , like IE. To send a POST to web server to acknowledge its close event. However, I just think, web server and client browser is a TCP connection. And if the client browser is close and it imply the TCP socket is...
6
5218
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
3758
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 understand it I need a PHP script that pulls the query and dumps the data into XML format, that is called by triggering a javascript event that reads that file with XMLhttprequest.
13
4864
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, suggestions or improvements? if( wmv file) document.write("<OBJECT id=Player classid=CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6 height="354" width="479">
1
2458
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 keypress var captureKeys = function(ev) { ev = ev || window.event; // gets the event in ie or ns kCode = ev.keyCode || ev.which; // gets the keycode in ie or ns
4
3086
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. What I have found is that if the page in Frame2 fails to load and I get an error page. when I detect an error page I want to set frame2.location.href = "about:blank" and then write a temp error page until the server becomes available again. The...
1
4145
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 ; Styleff_1024x768.css; Style_1280x1024.css ; Styleff_1280x1024.css ) I want to set css style file depends on resolution browser I wrote this script to detect type of browsers, but i don't have any idea
0
7894
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8284
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8392
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6711
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2410
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.