Including the cross-browser objects into a page
February 1st 1998
If a Web page needs to provide cross-browser functionality, both source code files need to
be included within the page. If the page only needs to support one browser or another, only
one file needs to be included. Why use a cross-browser object for a browser-specific page?
With the use of cross-browser objects, an added advantage is that if the browser alters its
DHTML implementation I need only make the change within the cross-browser code and not
in all of the applications using the DHTML functionality. With all of the efforts
currently underway at the W3C on the Document Object Model standard (accessible from
the resource section), this centralization of the browser-specific functionality is a good
idea even if the site is not creating cross-browser objects.
For now, the technique to include both browsers' objects into a page is:
<SCRIPT src="ns4_obj.js" language="javascript1.2">
</SCRIPT>
<SCRIPT src="ie4_obj.js" language="jscript">
</SCRIPT>
This technique includes JavaScript source into a page. The first source code file
included is the Navigator source code file, with a language of "javascript1.2".
The second is the IE source code file, with a language of "jscript".
As the objects and object instantiation
routines are named the same, what keeps each browser from picking up the wrong code?
First, Navigator will not pick up code from a scripting specification it does not
process, so it will not pick up the "jscript" source code. Now, IE 4.x does process
JavaScript 1.2 as well as JScript, and actually loads both files. However, if both
files contains functions with the same name, IE 4.x defaults to using the functions
in the scripting block that is included last in the page. As the IE 4.x cross-browser
object source code file is positioned after the Navigator specific source code file, IE 4.x
picks up the functions from the appropriate file. Based on this, it is essential
that the order taken for including these files be followed.
Once the script files are included into the page, the cross-browser objects are instantiated with
a call to wdvlCreateObjects. This function must be called after all
the elements on the page have been loaded to the browser, so I usually call this function
within the onLoad event of the page:
<BODY onload="wdvlCreateObjects()">
That's all it takes to create the cross-browser objects. Next up, time to create a test case
to make sure the objects have been instantiated correctly.
|