473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

A Guide to Coding Cross-Browser Scripts

acoder
16,027 Expert Mod 8TB
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 9919
acoder
16,027 Expert Mod 8TB
I've added your articles as Part 2 - Event Normalization. Thanks volectricity.
Jul 3 '07 #2
trbjr
35
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 Expert 4TB
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 Expert 4TB
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 Expert 1GB
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 Expert Mod 8TB
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 Expert Mod 8TB
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 Expert Mod 8TB
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.