<

HTA - Hypertext Application tutorial

Published on
24,883 Points
18,483 Views
4 Endorsements
Last Modified:
Approved
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 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.

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

function DoStartup() {  // called from BODY ONLOAD 
	SetWindowSize( gnWinWide, gnWinHigh ); // app init stuff
	document.title=gnWinCaption;
}
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>";
}
</script>

<!-- 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>
</DIV>
</body>
</html>

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;
	   oRs.MoveNext();
	}
	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
References
Here are some links to Microsoft that provide information useful in working with HTAs:

Overview:
   http://msdn.microsoft.com/en-us/library/ms536496(VS.85).aspx

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/en-us/library/ms536495.aspx

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
    http://msdn.microsoft.com/en-us/library/aew9yb99(VS.85).aspx
Run Method (Windows Script Host)
    http://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx

General reference for HTML objects:
HTML Objects
    http://msdn.microsoft.com/en-us/library/ms533054(VS.85).aspx

ActiveXObject Object (Windows Scripting - JScript)
    http://msdn.microsoft.com/en-us/library/7sw4ddf8(VS.85).aspx

Access files and directories from your HTA!
The FileSystemObject object
    http://msdn.microsoft.com/en-us/library/z9ty6h50(VS.85).aspx

ADO API Reference
    http://msdn.microsoft.com/en-us/library/ms678086(VS.85).aspx

Access the Registry:
RegRead Method  
    http://msdn.microsoft.com/en-us/library/x05fawxd(VS.85).aspx

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  
4
Author:DanRollins
Enjoy this complimentary article view.

Get unlimited access to our entire library of technical procedures, guides, and tutorials written by certified industry professionals.

Get 7 days free
Click here to view the full article

Using this article for work? Experts Exchange can benefit your whole team.

Learn More
COLLABORATE WITH CERTIFIED PROFESSIONALS
Experts Exchange is a tech solutions provider where users receive personalized tech help from vetted certified professionals. These industry professionals also write and publish relevant articles on our site.
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Learn from the best.