- Community Pick
Use HTAs for rapid testing, early development, and in-house deployment.
This article describes how to create an HTA (Hypertext Application). To illustrate a common scenario, we will open an ADO recordset and display some of it in an HTML table -- with only a few lines of script. We'll be using JavaScript (actually, Microsoft's JScript) because it's a flexible scripting language and web developers are usually quite familiar with it. A little note: Browser compatibility is irrelevant here -- an HTA is executed by a program named MSHTA.Exe, a Microsoft/Windows-specific
An HTA (Hypertext Application) is basically an HTML file with the added feature that it is not constrained by the browser's scripting "sandbox." It is a "trusted application" so it can access ActiveX objects and launch executables without displaying that irritating warning box popup.
USES:
- Throw together a quick proof-of-concept or a demo to exercise an ActiveX object.
- Provide a "front-end" U/I to set up a complex command-line needed to run command-line utility programs.
- Use as a generic "batch file" tool to perform a sequence of operations but with the ability to accept input interactively and display output.
- Provide a user-friendly U/I to drive such commonly-available application programs as Internet Explorer, Excel, Access, etc.; a way to use the COM-exposed functionality of the application without losing the user in the complex U/I of the program itself.
I keep the following snippet as a sort of template -- a starting point for my HTAs:
Copy that snippet to a text file on your desktop. Rename it to test.HTA. Double click it, to see:
You may wonder about that [restart] button. It's just a convenience for use while developing. You will be using your favorite text editor to modify the HTML and the script, and as you make changes, you would normally need to exit and restart the HTA (or right-click and choose Restart). This button lets you leave the window open as you make minor tweaks to the source code. Make a change, save it, then click the [restart] button.
Access a database and show output
Now, let's make the HTA do something useful. Replace the DoTest() function with the following:
That JScript code creates a couple of ADO objects. It accesses the SQL Server Northwind sample database and opens a recordset on its Customers table. It then cycles through the table and generates some HTML with some of the field data.
Getting User Input
It is very easy to add some input fields to the HTML and use the values. For instance, add this down near the bottom, where the HTML for the U/I lives:
And modify your DoTest() function and replace the var sSQL line with...:
Now the user can input a letter or two in the input box and the resulting output will contain only records in which the CompanyName begins with that (or those) letters.
References
Here are some links to Microsoft that provide information useful in working with HTAs:
Overview:
http://msdn.microsoft.com/
You can control some of the "look" and options of your HTA window (ignored here, because defaults work fine for the examples)
HTA:APPLICATION object
http://msdn.microsoft.com/
If you you want to launch a program with your HTA like this...
var oShell= new ActiveXObject("WScript.She
oShell.Run( "notepad.exe " + gsAnyTextTextFile );
... then see:
WshShell Object
http://msdn.microsoft.com/
Run Method (Windows Script Host)
http://msdn.microsoft.com/
General reference for HTML objects:
HTML Objects
http://msdn.microsoft.com/
ActiveXObject Object (Windows Scripting - JScript)
http://msdn.microsoft.com/
Access files and directories from your HTA!
The FileSystemObject object
http://msdn.microsoft.com/
ADO API Reference
http://msdn.microsoft.com/
Access the Registry:
RegRead Method
http://msdn.microsoft.com/
=-=-=-=-=-=-=-=-=-=-=-=-=-
If you liked this article and want to see more from this author, please click the Yes button near the:
Was this article helpful?
label that is just below and to the right of this text. Thanks!
=-=-=-=-=-=-=-=-=-=-=-=-=-
by: aikimark on 2010-04-11 at 15:33:02ID: 12949
@Dan
What is the problem with VB or perl? The Microsoft HTA tutorial pages use VBScript code.