HTA Tutorial #1
To follow this tutorial, you will first need to read the HTML Applications documentation on the HTML Applictions ntroduction page. You will also need access to a Windows PC running Internet Explorer 5.0 or above. It will be necessary to construct a basic template file to make creating new files so much easier. This file will look something like this:-
<html>
<HTA:APPLICATION>
<SCRIPT Language="VBScript">
<!--
Option Explicit
-->
</SCRIPT>
<body>
</body>
</html>
You will notice that it is an HTML file despite
the fact that a WEBlication is run locally and not
on the Internet. Even though a WEBlication acts
like an ordinary Windows program, it is actually still being run
in a browser window albeit a specialised one. This allows us to
use all of the active content and GUI widgets you associate with
the web without actually having to run it over a connection or
via a local web server. I'll go through the file a line at a time.
<html>
It's an ordinary web page which always starts with this tag to tell the browser to treat it as command rather than just plain text.
<HTA:APPLICATION>
This is the important bit. If you leave out this tag then some
things such as message boxes will refuse to work with the file
named with an extension of .HTA but will work in an ordinary
browser window with a .HTM extension. I'll go through all this
again further down.
<SCRIPT Language="VBScript">
Another important tag. Without this, the browser will interpret all the code as simple text rather than as a program. We are using Visual Basic Script here because I find it easier to use and many of you will be familiar with it or perhaps with older versons of BASIC. You could use JavaScript here if you are more familiar with it but all the code I will be showing you will have to be converted (although you can mix the two if you want - a subject for another tutorial).
<!--
Not strictly necessary but useful if you want to run it in a proper browser window. Everything after this and before the next tag will be interpreted as program source code and will produce an error if it isn't. More on that later.
Option Explicit
Again, not strictly required but it will produce an error if a variable has not been declared which I consider to be good programming practice. Leave it out if you're not bothered (although don't blame me if you get some funny bugs).
-->
End of the script.
</SCRIPT>
Close the script section.
<body>
Now the HTML proper can be specified. More later.
</body>
</html>
Close the main body of the HTML and close the file itself. Here's that file in full again.
<html>
<HTA:APPLICATION>
<SCRIPT Language="VBScript">
<!--
Option Explicit
-->
</SCRIPT>
<body>
</body>
</html>
Cut and paste this into a file and name it Template.HTA. Double click on it in Windows Explorer and check there are no errors and that a window is displayed. If you make a copy of it and name it template.htm and remove the tag:-
<HTA:APPLICATION>
then it will run in a standard browser window. The advantage of
using .HTA is that no security messages will be
displayed once we start adding code which we will do now. Make a
copy of template.hta and rename it to Msgbox.HTA.
Between the two code tags add:-
msgbox "Hello"
This will make the file look as follows:-
<html>
<HTA:APPLICATION>
<SCRIPT Language="VBScript">
<!--
Option Explicit
msgbox "Hello"
-->
</SCRIPT>
<body>
</body>
</html>
Save it and run it. You should see a window containg a message
box containing a greeting. Click OK. Note that
the window remains. Pressing F5 will refresh it
and run the program again. Click OK and close
the window in the normal way. Congratulations. You have written
your first WEBlication.
One thing needs to be added for completeness and that is some comments about the program, who wrote it, the date and perhaps a description of what it does. Probably not required on such a small program as this but it is a good habit to get into. Take a look at this:-
<SCRIPT Language="VBScript">
<!--
' Author:- Nick
' Date:- Now
' Description:- Hello program
' Comments:- Pretty simple eh?
Option Explicit
REM This is a comment
MsgBox ("This is code")
' This is also a comment
-->
</SCRIPT>
I have ommitted all the HTML stuff for clarity but it will need to be included when the file is created. There are several ways of showing comments one of which is the REM or Reminder statement. Really a hangover from previous versions of BASIC and not really used anymore. The other is an apostrophe as shown above. Comments are part of the code and so must be inside the script tags. They can also be added between lines of code to explain what is happening although excessive use of comments can slow the code down a bit - not a problem on new machines but could give your users a problem if they are using older kit. The program is now complete.
If you want to know more then
view the next tutorial from the main menu.