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

Home Posts Topics Members FAQ

A Guide to Coding Cross-Browser Scripts

acoder
16,027 Recognized Expert Moderator MVP
It's strange, isn't it? After so many years of standards, you would've thought that you could write a simple piece of script that would work in all browsers. Alas, this is not always the case. With browsers adding their own proprietary stuff and actually failing to implement standards (one, in particular, being quite notorious), it just makes things harder than they should be.

However, it's not all bad. You can still write scripts which work in pretty much all browsers. OK, I'll qualify that - all modern browsers.

Having seen some of the horrible proprietary code still floating around, I thought it would make sense to have a collection of short articles that help to write code that can work in all browsers. This should include general guidelines and code snippets.

In Part 1 - Browsers and Standards, we discuss why browser detection should be avoided, why object detection should be used and why we should use standards.

The W3C and Microsoft event object models are very different. In Part 2 - Event Normalization, volectricity describes how you can normalize them.
Jun 30 '07 #1
8 9922
acoder
16,027 Recognized Expert Moderator MVP
I've added your articles as Part 2 - Event Normalization. Thanks volectricity.
Jul 3 '07 #2
trbjr
35 New Member
Hi Acoder,

I found your idea and response from volectricity helpful, interesting and instructive.
May I ask (without being utterly dumb) that you post a link to the standards you refer to.

A google of "javascript standards" gives many hits. The purpose of my using this forum is to gain access to the experience of others, so it would be really helpful to be able to link to the site that reflects the standards you mean. I know about a few, but my lack of experience makes me wonder about a comprehensive source.

Hope this makes sense to you.

Thanks for the article as far as it has gone.

Best,

trbjr
Jul 23 '07 #3
Plater
7,872 Recognized Expert Expert
Been hunting around for hours for best ways to do cross browser for xmlHTTPRequest object and found a usefull snipit:

Expand|Select|Wrap|Line Numbers
  1. /*@cc_on @if (@_win32 && @_jscript_version >= 5) 
  2.    if (!window.XMLHttpRequest) 
  3.       function XMLHttpRequest() 
  4.       { 
  5.          return new ActiveXObject('Microsoft.XMLHTTP') 
  6.       } 
  7. @end @*/ 
  8.  
This allows you to simpley use:
Expand|Select|Wrap|Line Numbers
  1. var myhttp = new XMLHttpRequest();
  2.  
in most browsers (Even in IE5 and later)
(I borrowed it from here)

It doesn't deal with the netscape securities, but that can be dealt with later.
Jul 23 '07 #4
Plater
7,872 Recognized Expert Expert
As an after thought, I made this:
Expand|Select|Wrap|Line Numbers
  1. if (!document.getElementById)
  2. {
  3.     document.prototype.getElementById = function() 
  4.     {
  5.         if (document.all)
  6.         {
  7.             return document.all[id];
  8.         }
  9.         else if (document.layers)
  10.         {
  11.             return document.layers[id]; 
  12.         }
  13.     }
  14. }
  15.  
But I have no idea what browser to use that DOESN'T have getElementById() to test it out on
Jul 25 '07 #5
kovik
1,044 Recognized Expert Top Contributor
But I have no idea what browser to use that DOESN'T have getElementById() to test it out on
By today's standards, we can ignore any browser that does not support getElementById(), and this has safely been the case for over a year now.

And for creating the AJAX object, the "best" method would include try...catch statements, as failed object creation throws and exception. Even if the browser supports it does necessarily mean that it will be created properly, and not every browser supports AJAX. Mozilla and Microsoft were ahead of the game with AJAX technologies. If course, the snippet you posted before the last one could be incorporated into the try...catch, but a more "standardized" method would be:

Expand|Select|Wrap|Line Numbers
  1. function GetRequestObject()
  2. {
  3.     try
  4.     {
  5.         return new XMLHttpRequest();
  6.     }
  7.     catch($e)
  8.     {
  9.         try
  10.         {
  11.             return new ActiveXObject('Microsoft.XMLHTTP');
  12.         }
  13.         catch($e)
  14.         {
  15.             return null; // The request object could not be created
  16.         }
  17.     }
  18. }
Jul 28 '07 #6
acoder
16,027 Recognized Expert Moderator MVP
Hi Acoder,

I found your idea and response from volectricity helpful, interesting and instructive.
May I ask (without being utterly dumb) that you post a link to the standards you refer to.

A google of "javascript standards" gives many hits. The purpose of my using this forum is to gain access to the experience of others, so it would be really helpful to be able to link to the site that reflects the standards you mean. I know about a few, but my lack of experience makes me wonder about a comprehensive source.

Hope this makes sense to you.

Thanks for the article as far as it has gone.

Best,

trbjr
For the DOM, you can see the W3C DOM (warning: not an easy read).

As for Javascript (or ECMAScript), see the language specification.
Aug 8 '07 #7
acoder
16,027 Recognized Expert Moderator MVP
For the DOM, you can see the W3C DOM (warning: not an easy read).

As for Javascript (or ECMAScript), see the language specification.
Added these links and others to the main article.
Aug 22 '07 #8
acoder
16,027 Recognized Expert Moderator MVP
But I have no idea what browser to use that DOESN'T have getElementById() to test it out on
You'd be looking at the old buggy browsers at the height of the first browser war: IE4 and Netscape 4.
Aug 31 '07 #9

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

Similar topics

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...
3
by: phanimadhav | last post by:
Hi, i am working on the coding part which is based on jsp/html.As im fresher i am facing some problems with the code.so,can u please guide me..This is the error which i ve got...please have a...
3
wiredwizard
by: wiredwizard | last post by:
So does my site need optimization or marketing? (my response to a recent email from a gentleman asking whether he should optimize or market his website and I was wondering what others think) ...
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
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...
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...
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: 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...
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
muto222
php
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.