HTA - Hypertext Application tutorial

Published on
22,370 Points
4 Endorsements
Last Modified:
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 program.

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.

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.
Sample HTA shows some ADO outputI keep the following snippet as a sort of template -- a starting point for my HTAs:
var gnWinCaption= "HTA tester";
var gnWinWide= 400;
var gnWinHigh= 300;

function DoStartup() {  // called from BODY ONLOAD 
	SetWindowSize( gnWinWide, gnWinHigh ); // app init stuff
function SetWindowSize(w,h) {  // known bug in mshtml; try/catch fixes
	try     { window.resizeTo( w, h );} 
	catch(e){ SetWindowSize(w,h); }  //  do it until it works
function DoTest() 
	// instantiate and exercise ActiveX objects, etc.
	oDivDisplayArea.innerHTML="<b>test complete!</b>";

<!-- I like to put the U/I stuff at the bottom -->
<!-- ***************************************** -->
<body onLoad='DoStartup();'>
<DIV align=right><input type=button value="restart" 
    onclick= "document.location.reload();"></DIV>

<input type=button value='DoTest' onclick='DoTest();' </input>
<DIV ID=oDivDisplayArea>
   <font color=gray>(stuff will be displayed here)</font>

Open in new window

Copy that snippet to a text file on your desktop.  Rename it to test.HTA.  Double click it,  to see:
The "minimal HTA" templateYou 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:
var goDb;
var gsConnect= "DSN=Northwind;UID=;PWD=";  

var adOpenStatic=   3;   // constants used in oRs.Open(...)
var adLockReadOnly= 1;
var adCmdText=      1;
function DoTest() 
	var sSQL="SELECT * FROM Customers ORDER BY CustomerID";
	//--------------------------------------- open the db connection
	goDb= new ActiveXObject( "ADODB.Connection" );
	goDb.Open( gsConnect );
	//---------------------------------------- open the recordset
	var oRs= new ActiveXObject( "ADODB.Recordset" );
	oRs.Open( sSQL, goDb, adOpenStatic, adLockReadOnly, adCmdText );

	var sOutput="<TABLE border=3>";
	while (! oRs.EOF ) {
	   var sOneLine= "<TR>";
	   sOneLine += "<TD>"+ oRs.Fields('CustomerID' ).Value + "</TD>";
	   sOneLine += "<TD>"+ oRs.Fields('CompanyName').Value + "</TD>";
	   sOneLine += "<TD>"+ oRs.Fields('City'       ).Value + "</TD>";
	   sOneLine += "</TR>";
	   sOutput += sOneLine;
	sOutput += "</TABLE>";
	oDivDisplayArea.innerHTML= sOutput; // show the output

Open in new window

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:

Enter Key: <input type=text id='edKey'</input>

Open in new window

And modify your DoTest() function and replace the var sSQL line with...:
var sKey=  document.all.edKey.value;  // get user input
var sSQL="SELECT * FROM Customers"
          +" WHERE CompanyName LIKE '" +sKey+ "%' "
          +" ORDER BY CustomerID"; 

Open in new window

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.
ADO Output filtered by user input
Here are some links to Microsoft that provide information useful in working with HTAs:


You can control some of the "look" and options of your HTA window (ignored here, because defaults work fine for the examples)


If you you want to launch a program with your HTA like this...
      var oShell= new ActiveXObject("WScript.Shell");
      oShell.Run( "notepad.exe " + gsAnyTextTextFile );
... then see:

WshShell Object
Run Method (Windows Script Host)

General reference for HTML objects:
HTML Objects

ActiveXObject Object (Windows Scripting - JScript)

Access files and directories from your HTA!
The FileSystemObject object

ADO API Reference

Access the Registry:
RegRead Method  

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!
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 45

Expert Comment


What is the problem with VB or perl?  The Microsoft HTA tutorial pages use VBScript code.
LVL 49

Author Comment

HTAs can certainly use VBScript -- and, I suppose, Perl or any other language that is supported by the MSHTA host.  I am most familiar with JavaScript for accessing the browser DOM and its syntax is familiar to most web developers, so I chose it for my examples.

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Join & Write a Comment

Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to dynamically set the form action using jQuery.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month