home / experts / javascript / pharmacy / article2 |
|
One of JavaScript's most important features is the ability to embed external scripts. External scripts are extremely useful for Dynamic HTML applications, which require a fourth-generation browser -- all fourth-generation browsers support external scripts, so you don't even need to worry about backward compatibility. In theory, cross-browser scripts can be twice as large as a single-browser version of the same script. In long scripts, size really does make a difference, because the browser must download the entire file in order to execute the script. An easy solution is to dynamically insert the external script, depending on the browser:
Looking back at our simple example from the previous page, each of the preceding external files (ns4version.js and ie4version.js) should include a distinct version of the
And ie4version.js should include the following function:
Notice that both functions have the same name and feature the same parameters. This is very important, because a consistent interface ensures simple implementation of the function in both browsers. When you call the function from the main document, you don't really care what browser the user is running. The different function bodies take care of the dirty work. Instead of placing these functions in different files, we can use another technique where both can be placed in the same script (but with different names):
Notice that both functions still have the same parameters -- an identical interface. Now all we have to do is assign them both a common name, so they can be invoked the same way in Navigator 4.0x and Internet Explorer 4.0x:
That's all there is to it. You can now call the Since a function reference is an object, it can be assigned to variables, handed to other functions, and handled just like any other object. However, it can also be invoked by applying a set of parentheses to the reference. In our example, we assign a reference of the appropriate function to a global variable named With this interesting technique our short article comes to an end. After reading the tutorial, you should be familiar with three different approaches to cross-browser scripting. |
Created: June 4, 1997
Revised: June 4, 1997
URL: http://www.webreference.com/js/pharmacy/article2/other.html