1. Home
  2. Computing & Technology
  3. JavaScript

Cross Browser DOM Script

Join the Discussion

Questions? Comments?

Related Tutorial

Tutorial

If you are creating your own dynamic HTML scripts then you will need to work out which document object model a given browser supports if your script is to function in that browser. All version 5+ browsers support the standard DOM but there may still sufficient visitors still using Netscape 4 to make it worth supporting that DOM as well. This code does that for you and also provides a function that you can call that will allow your script to work with whichever of the DOMs that the browser supports.

The code to support the Internet Explorer 4 DOM is commented out in the code as statistics I have seen suggest that no one uses that browserr any more. If your statistics show differently then uncomment the code, otherwise you can delete the /* */ commented code.

This code should be incorporated into a script in the head section of your page.

Once we have incorporated this code into our script we can then reference the objects on our page as follows. To reference the ordinary properties and methods (assuming we have labelled the object 'myobj'):

var obj = xDOM(myobj,0);
var myval = obj.value;

The only change we need to make to reference the style properties is to replace the 0 with a 1.

var objS = xDOM(myobj,1);
var mytop = objS.top;

For more information see the Cross Browser DOM tutorial.

Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.