473,468 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A Guide to Coding Cross-Browser Scripts Part 1: Browsers and Standards

acoder
16,027 Recognized Expert Moderator MVP
Do not use browser detection - use object detection instead

Browser detection is when you attempt to detect a browser. This is unreliable and has many problems. What if a new browser comes along? Can you really expect to detect every single browser on the planet? I have even seen scripts that try to detect browser versions. Imagine their annoyance and distress when IE7 came out ("It works fine in IE6, but won't work in IE7"). So what's the solution?

Easy, use object detection instead. In object detection, we check for the existence of an object. If it exists, we code with it. If not, we can try something else or maybe check for the existence of another object. This way we can support as many browsers as possible without having to worry about what a particular browser may support. For example, in AJAX, we can check for the existence of the XMLHttpRequest object:
Expand|Select|Wrap|Line Numbers
  1. if (window.XMLHttpRequest) { ...
In IE6, this is not natively supported, so we have another code block which checks for the support of the ActiveXObject:
Expand|Select|Wrap|Line Numbers
  1. else if (window.ActiveXObject) {
Hopefully, this gives you a basic idea. Use the standard methods first. The standard methods usually work in almost all modern browsers.


Use the standards

Standards are there for a reason. Use them. This will make your code work in most browsers. If, for some reason, you're coding the standard way and your code isn't working, chances are that the browser will be Internet Explorer.

However, since it is the most popular browser, you have no choice but to support it. The good news is that being popular means that the bug/quirk will most probably be documented (not in the official documentation, but somewhere on the Web).

Check for the existence of the object, method, etc. and code accordingly. If it's simply a bug, then use try..catch statements to catch the error in the buggy browser and use the fix/solution. If there's a solution which doesn't affect any other browser, you could use that instead.

As an example, take the case of appending an option at the end of a select object. The standard method uses two arguments - the second one being null, but that won't work in IE, so you can use the following code:
Expand|Select|Wrap|Line Numbers
  1. try {
  2.   selObj.add(optObj, null);
  3. } catch(e) { // for IE
  4.    selObj.add(optObj);
  5.   }
What are the actual standards? For the Document Object Model (DOM), you can see the W3C DOM. As for Javascript (or ECMAScript), see the language specification.

If you find these difficult to decipher, the W3Schools HTML DOM reference points out if something is part of the W3C standard. The Mozilla Core Javascript 1.5 Reference is also useful for the core language.


There are more than two browsers

"Really?!", you may ask sarcastically. However, judging by questions commonly asked in the forums, many seem to only worry about Internet Explorer and Mozilla Firefox. Some, it's sad to say, still think it's enough to support Internet Explorer only.

Have a look at a list of browsers to see that there are a lot more than just two.

A web application or a website of any sort should attempt to support as many web browsers as possible. Again, as mentioned earlier, if you follow the standards, this task will be made a lot easier. All you may need to do is to adjust slightly to allow for a few bugs or quirks.
Jul 2 '07 #1
0 4320

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
1
by: Donald Smith | last post by:
Hey, Can anyone tell me how to using cross-coding in .Net? Like In a C# project file, being able to use a VB.NET module. Thanks. Donald
4
by: Mikkel christensen | last post by:
Hi there I wonder if any of you could point me in a direction where I can find some usefull information about coding standarts. I have some generel experiense in programming but I lack many...
7
by: john_williams_800 | last post by:
Hi; I am thinking of working on a site for a local nonprofit group. Since I will have the option of starting from the beginning I thought I would look into making the site Bobby compliant. ...
23
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the...
0
by: saravanansvks | last post by:
Dear Friends, I have prepared a programme in Visual Basic for SHAPE OPERATOR.But my coding are changing the shape only.It does not changes the fill style of the shape tool .And it is not giving ant...
4
by: AzizMandar | last post by:
C++ Event Coding Questions I have done some simple programs in C++ and read a lot of good C++ books (Including The C++ Programing Language, and C++ Primer) I am trying to understand and...
10
by: ryann18 | last post by:
I have a midterm tomorrow and I need some help! I am not good at Java at all I have the worst teacher on the face of the earth! He tells not teaches! But can someone give me some pointers!! He...
2
by: telsave | last post by:
Hi Hope someone out there can help me. I have just developed my first WEB page to sell an ebook I have written. I have of course included a squeeze page to capture details but what I am looking for...
6
by: ampo | last post by:
Hello. Can anyone help with cross-domain problem? I have HTML page from server1 that send xmlHTTPRequest to server2. How can I do it? Thanks.
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
1
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.