During the last few weeks, while building HTML Applications (HTAs), I encountered some snags that have caused me to stop and think more about exactly how browsers interpret markup and scripts. In dealing with the latter, this article will highlight some of the ramifications of mixing scripting languages in your web pages. Although these discussions will be applicable to cross-browser scripting in general, many of the points will be relevant to HTAs as well.
Supported Languages
Some scripting considerations do apply to HTAs because they present content using the Internet Explorer browser. As such, HTAs can be written in any scripting language which will work in Internet Explorer (IE): namely JScript and VBScript. Strictly speaking, JavaScript is NOT supported by IE. Unlike most browsers, Internet Explorer doesn't understand regular JavaScript. Instead it treats any JavaScript as if it was Microsoft's proprietary JScript language and processes it accordingly. That's why so many scripts have to include IE-specific code. Take the following function:
function addEventHandler( eventName, eventHandler )
{
var CAPTURING_PHASE = true, BUBBLING_PHASE = false;
if( window.addEventListener )
{
addEventListener( eventName, eventHandler, BUBBLING_PHASE );
}
else if( window.attachEvent )
{
attachEvent( 'on' + eventName, eventHandler );
}
}
In typical cross-browser fashion, the else if checks for the JScript attachEvent() function. Testing for the existence of a function or attribute has replaced browser sniffing techniques, as the introduction of newer browsers has made that technique unreliable. In either case, the result is functions that are split into two: one part for JavaScript, and the other for JScript. An alternate style is to simply put each script in separate JavaScript and JScript tags. The following page demonstrates how browsers will only run the languages that they support. Hence, Firefox and Opera ignore JScripts and VBScripts, while IE runs all of them:
A document with three SCRIPT types
The Default Scripting Language
At first glance there may seem to be some ambiguity between JScript and JavaScript execution in IE since it recognizes both. However, when faced with two identical functions, IE's answer to the dilemma is to choose the default scripting language. Other browsers work much the same way. Have you ever noticed that you can create a script without a language or type attribute? If not, try it now. Write a simple script and remove the type tag, so that only the bare
|