Mixing Scripting Languages | 2 | WebReference

Mixing Scripting Languages | 2


Mixing Scripting Languages [con't]

Trouble can crop up within intrinsic event handlers anytime specific language features are included because the interpreter that runs the code may not be the one that you are expecting. For instance, multiple commands can be executed in both JScript and JavaScript by separating them with a semi-colon(;):

If you have a VBScript anywhere in your document, an error is thrown by the browser because it is using VBScript to execute the code. Luckily, there are a few simple ways around this issue. The first is to include the Language="JavaScript" attribute in the element tag, to specify the scripting engine to use:

Another way to accomplish the same thing is to preface the executable code with the language, followed by a colon(:):

The above solution may look familiar to you because it is the same syntax that is used to execute code from a link:

Just as browser sniffing subroutines have been replaced by cleaner and more effective methods, we are now seeing a gradual diminishing of intrinsic event handling from within element tags. Today's development standards aim for the separation of content (HTML markup), appearance (Cascading Style Sheets [CSS]), and behavior (scripts); a goal with which traditional intrinsic event handlers are acutely out of step. Modern web documents link elements with their associated handlers via event binding, which is possible because most all of the document properties and events are exposed via the Document Object Model (DOM). Perhaps you've seen something like the following before:

An even better method for binding event handlers to document elements is to use the DOM's addEventLister() and the JScript attachEvent() methods. These functions are far superior to assigning handlers directly to the event property because they add the handler to an array, rather than replace the existing one. We already saw some code that uses these methods at the beginning of this article. It used the typical if else structure to choose between JavaScript and JScript. Knowing what we do about browser differences gives us the competence to separate the handlers for each language, but with one challenge remaining: IE will try to run both the JScript and JavaScript code! What we need to do here is insert one script depending on the browser, but without resorting to browser sniffing. It turns out that there is a surefire way to identify IE, using conditional comments. They are a proprietary Microsoft syntax for including IE-only code in a document. They look a lot like regular HTML comments, so that other browsers will ignore them, but in IE, any code that appears between them, whether HTML markup or scripting, is interpreted by the browser. We can use that to set a flag and then dynamically insert the appropriate script:

The <SCRIPT> tag will either import the ie_script.js file:

...or the dom_script.js file:

Note that using document.write() is only one of several ways to dynamically append a script to a document. For other ways, see my On-demand JavaScript article.

Having the option of mixing multiple scripting languages within a document will allow you to capitalize on each language's strong suit while avoiding potential snafus. As we'll see next time, VBScript in particular, has some very powerful capabilities when used within an HTA, such as database, file system, and even registry access!

Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. In 2007, Rob recorded the KNIGHTFALL CD in collaboration with the former Ivory Knight vocalist and legendary guitarist/producer, Jeff Waters of Annihilator fame. A completely FREE high quality MP3 download of his "Ultraviolence" intrumental, is availalable from his website, www.robgravelle.com. Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: October 16, 2009