473,545 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

browser tests

What is the recommended code to test browser type, for conditional
processing in Javascript? I have seen tests for "document.a ll". I have
seen tests for the actual browser name, or substrings in the browser name.
What do you all recommend? Thanks...Jim...
Jul 23 '05 #1
8 1671
Jimnbigd wrote:
What is the recommended code to test browser type, for conditional
processing in Javascript? I have seen tests for "document.a ll". I have
seen tests for the actual browser name, or substrings in the browser name.
What do you all recommend? Thanks...Jim...


<URL:http://www.jibbering.c om/faq/#FAQ4_26>

The name of the game is 'feature detection'. Even if you could
reliably detect the browser from the UA string, you would then make
assumptions about what that particular browser does or doesn't support
that could be equally erroneous.

The most common result of browser sniffing is the possible exclusion of
any browser that you fail to take into account, even if it does support
the features you are using.

And that is the primary reason why browser developers faked UA strings
in the first place (heck, IE used to masquerade as Netscape).
--
Rob
Jul 23 '05 #2
Zif
RobG wrote:
[...]
And that is the primary reason why browser developers faked UA strings
in the first place (heck, IE used to masquerade as Netscape).


Much to the chargrin of those who wish it to be reliable for perfectly
honourable reasons.

--
Zif
Jul 23 '05 #3
Jimnbigd wrote:
What is the recommended code to test browser type, for conditional
processing in Javascript?
There are no recommended tests for browser type or version. Indeed the
recommendation is not to care about the browser type or version at all,
because it is impossible to acquire that information.

Conditional execution tests are determined by reason for making the
execution conditional. So, for example, if one branch in code is going
to use a set of features that are only supported by a sub-set of
browsers then the test that controls the branching should be verifying
the existence of those features in the current execution environment
(and possible aspects of the behaviour of those features). So, for
example, given a desire to read the scroll offsets of a document, one
group of browsers will make that information available as global
pageXOffset
and pageYOffset properties, which would be numeric where implemented and
undefined otherwise. The test would be:-

if('number' == pageXOffset){
...// code that reads global pageX/YOffset
}

- and the block of the if statement would never be executed in an
environment that does not implant the specific feature of interest.
And - else - branches with other conditional tests could test for
alternative features and conditions that apply to other environments,
again testing for the features/conditions that represent the reason for
the existence of the branches.
I have seen tests for "document.a ll".
With the exception of code that tests for - document.all - as a
condition that has to be satisfied prior to the use of -
document.all -(only), that is termed "object inference"; The inferring
of all the features of an object model form a test on one particular
feature. Object inference is an invalid strategy because it is based on
invalid assumptions (that are more often than not actually untrue in
reality). The inference that a browser is Microsoft IE based on the
environment implementing - document.all - is particularly bogus.
I have seen tests for the actual browser name, or
substrings in the browser name.
There is no available DOM property that represents the browser name. The
nearest being the much spoofed - navigator.appNa me -. Usually these
tests are based on the navigator.usetA gent - property, which reflects
the HTTP User-Agent header being used by the browser. HTTP 1.1
User-Agent headers are not specified as providing any information about
the client. And in reality User-Agent headers of different browsers are
often indistinguishab le; they cannot be examine to provide
discriminating information, so the User-Agent header cannot be used to
identify the browser type or version.
What do you all recommend?


Feature detection, as no browser detection strategy works and in the
vast majority of cases feature detection does. Making Feature detection
a far superior strategy.

Richard.
Jul 23 '05 #4
Zif wrote:
RobG wrote:
[...]
And that is the primary reason why browser developers
faked UA strings in the first place (heck, IE used
to masquerade as Netscape).

IE still does masquerade as Netscape 4, it just does it in a way that
makes it obvious that it is really not Netscape 4, at least by default.
Much to the chargrin of those who wish it to be
reliable for perfectly honourable reasons.


Not to the chagrin or those who wish to be reliable. Only to the chagrin
of those who wish to be reliable and have not yet learnt how to do that.

Richard.
Jul 23 '05 #5
Richard Cornford wrote:
<snip>
if('number' == pageXOffset){


Should be:-

if('number' == typeof pageXOffset){
^^^^^^
Richard.
Jul 23 '05 #6
Richard Cornford wrote:
Object inference is an invalid strategy because it is based
on invalid assumptions (that are more often than not actually untrue
in reality).


I've been working on a generalized (cross-browser) way of find the absolute
coordinates of an object on the page. There are a lot of quirks with
different browsers and CSS/positioning, and I've found no positioning script
so far that correctly considers objects with overflow:scroll when
calculating screen position (among other things). I've found that in order
to be accurate in as many browsers as possible, I need to use
feature/objection detection and infer other things based on that.

For example, looking for window.opera or document.compat Mode and inferring
different quirks about how object positions are calculated. Opera already
consider's an element's scroll when it has overflow:scroll into the
offsetLeft/offsetTop values, while other browsers do not. So for other
browsers, you need to consider the scrollLeft/scrollTop values, while for
Opera you do not. I know of no way to "detect" this behavior other than
knowing a browser's behavior and attempting to infer that the browser in use
exhibits this behavior by examining its DOM.

Do you have a better idea?

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #7
Richard Cornford wrote:
Feature detection, as no browser detection strategy works and in the
vast majority of cases feature detection does. Making Feature detection
a far superior strategy.


Makes sense to me. I have always wondered about some of the scripts I have
seen that test for "document.a ll". Who knows -- Some day Netscape or
another browser might decide to support "document.a ll", and then everything
breaks. Thanks...Jim...
Jul 23 '05 #8
Jimnbigd wrote:
Richard Cornford wrote:
Feature detection, as no browser detection strategy works and in the
vast majority of cases feature detection does. Making Feature detection
a far superior strategy.

Makes sense to me. I have always wondered about some of the scripts I have
seen that test for "document.a ll". Who knows -- Some day Netscape or
another browser might decide to support "document.a ll", and then everything
breaks. Thanks...Jim...


Case in point: Firefox already has 'undetectable' support for
document.all in quirks mode (and will also access elements by name & id
in global scope - send shivers up your spine?).
--
RobG
Jul 23 '05 #9

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

Similar topics

12
10134
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical scrollbar, you get the height of the entire document, screwing up any chance of centering a window in the browser using these values. Is there a...
17
2526
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this with a link to old information that suggests use of "navigator": http://developer.irt.org/script/43.htm
3
1343
by: Dot net work | last post by:
Is there a resource on the internet to tell me which javascript can work with which browser. For instance, can I use style.color with all browsers - that kind of thing. Thank you, Regards, dnw.
0
348
by: fishball79 | last post by:
Hi all, I'm encountering a weird problem. I am attempting to run NUnit tests on web services (developed on .net framework 1.1) hosted on a seperate machine on the intranet. I can access the .asmx files via HTTP with my web browser, yet I get gateway timeouts when I run NUnit tests. I have disabled the firewall, added the server ip to...
7
1920
by: phal | last post by:
Hi I think there are many different browsers to browse to the Internet, how can I write the javascript to identify different browser and display according to the users. Some browser disable the javascript by default or by the user, how can i solve this problem if the javascript is disable. thank you
16
2334
by: petermichaux | last post by:
Hi, Does anyone have a a cross-browser setOpacity function that does not use browser sniffing? I looked at the Yahoo! UI function and it detects IE by looking for window.ActiveXObject. I also looked at Scriptaculous and it uses navigator.userAgent. Thanks, Peter
27
2695
by: David Golightly | last post by:
This is just a quick poll for all you web devs out there: What browsers do you test on/are concerned about compatibility with? Obviously, you're going to test on current-generation browsers such as IE6, IE7, Firefox 1.5/2, Opera 8/9, Safari 2, etc. How old must a browser be before you stop worrying about it? Anybody here still test on...
10
3241
by: Conrad Lender | last post by:
In a recent thread in this group, I said that in some cases object detection and feature tests weren't sufficient in the development of cross-browser applications, and that there were situations where you could improve the application by detecting the browser vendor/version. Some of the posters here disagreed. Since then, I've had to deal with...
0
7420
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...
0
7680
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. ...
0
7934
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...
1
7446
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...
0
6003
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...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3476
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...
1
1908
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
1033
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.