HTA Application ~ Javascript & Simple HTML Form

An HTML form is the user inferface for your HTA application. On my HTA forms, I use Javascript to set or get form element values because I've been using Javascript for years on my web forms. If you have no previous scripting experience, you might just want to look for complete VBScript solutions. If you're comfortable with Javascript, Perl, or any other language supported by .NET Framework, then go with that.

I use Javascript to communicate with the form and VBScript for everything else, including reading and writing files and folders, working with text files, communicating with Windows or the Registry, and working with remote computers.

Because VBScript will not work in non Internet Explorer browsers, for this tutorial, I'll be using Javascript only.

We begin by adding a form with a simple form tag. On an HTA, no form action or method is needed like you would normally use on a web page. No form name is needed unless we have more than one. Now we add our button. The button's Javascript onclick event will trigger the action. Here's the code for our form which has a yellow background below. Add the following within the body tags beneath the </p> tag.

<form>
  <input type="button" name="btnStart" value="Start" onClick="Validate()" />
</form>

Planning ahead, we're going to add 2 Javascript functions. Validate will make sure data has been entered, then call the RunScript function if data was entered. Now replace the Javascript section in simple.hta which should now look like this:

<script language="javascript" type="text/javascript">
  window.resizeTo(640,480);
  function Validate() { // check required fields and/or valid data
   RunScript();
  }
  function RunScript() { // do something
    alert("This form works.");
  }
</script>

Right now, Validate does nothing but call RunScript. We'll be adding on to Validate as we continue.

Please note: this is only a demonstration of using a form on an HTA, but the functionality should be the same as simple.hta, except resizing the window.

Click the Start button to test the form.