|
|
|
Windows HTAs are very simple to create, but can be made into powerful and useful tools. This short tutorial shows how to quickly obtain stock quotes from Yahoo! Finance.
Every HTA (HTML Application) consists of three sections:
This short tutorial will show how just quickly these three sections can be combined to produce a useful desktop application - in this case a desktop application for obtaining stock quotes from Yahoo! Finance. The Head SectionThe head section of an HTA is used to set up it's general properties, and these are enclosed within the HTA:APPLICATION tag: <head>
<HTA:APPLICATION
ID="yahoohta"
APPLICATIONNAME="Yahoo! Finance Stock Quotes"
SCROLL="yes"
SINGLEINSTANCE="yes"
>
</head>
In this examle:
The Script SectionThe script section is, of course, where all of the functionality of the HTA resides. This particular application uses VBScript, but it could just as easily have been written using Javascript. The Application itself will:
Part of the process will be to format the Yahoo! Finance url, which must be in the form: http://download.finance.yahoo.com/d/quotes.csv?s=[symbols]&f=[fields]&e=.csv
This is a task that can be achieved relatively easily with VBScript: <script language="VBSCRIPT">
document.title = yahoohta.APPLICATIONNAME
field_array = array ( _
array ("c1","Change in Stock Quote"), _
array ("d1", "Date"))
company_array = array ( _
array ("MSFT","Microsoft"), _
array ("NOVL","Novell Inc."), _
array ("RHT", "Redhat Inc."))
Here, the company and field details are hard coded into arrays, but they can easily be read from files or a database; wherever these arrays come from they can be used in subroutines to format the information for use in the body section: Sub field_check_list
document.write "<table>"
For Each member In field_array
document.write "<tr><td><input id=field type=checkbox " _
& "value=" & member(0) & " checked></td>" _
& "<td>" & member(1) & "</td></tr>"
Next
document.write "</table>"
End Sub
Sub company_check_list
document.write "<table>"
For Each member In company_array
document.write "<tr><td><input id=company type=checkbox " _
& "value=" & member(0) & " checked></td>" _
& "<td>" & member(1) & "</td></tr>"
Next
document.write "</table>"
End Sub
The next subroutine will:
Sub run_vbscript_button_click
on error resume next 'VBScript has no on error goto statement
In this example there is a default set of fields to be retrieved - the company symbol and the latest stock quote: fields = "sl1"
The list of additional fields is obtained from the check boxes that the user selects: For Each member In field
If (member.checked = true) Then
fields = fields & member.value
End If
Next
As is the list of companies to be used: symbols = ""
For Each member In company
If (member.checked = true) Then
If (symbols = "" ) Then
symbols = member.value
Else
symbols = symbols & "," & member.value
End If
End If
Next
The url can now be constructed from the field and company lists: url = "http://download.finance.yahoo.com/d/quotes.csv?" _
& "s=" & symbols _
& "&f=" & fields & "&e=.csv"
Next the url is sent to the server using the XML HTTP object: Set xmlhttp = CreateObject("microsoft.xmlhttp")
xmlhttp.open "get", url, false
xmlhttp.send ""
Then the response from Yahoo! Finance must be processed: 'Remove unwanted speech marks:
ip_data = Replace(xmlhttp.responseText,"""","")
'Load each kine of the data into an array:
ip_data = split(ip_data, vbcrlf)
op_data = "<b>Stock Quotes</b><table>"
For Each line In ip_data
op_data = op_data & "<tr>"
'Split each line of data into an array of fields:
line_data = split(line, ",")
'Then display the fields in a table:
For Each member In line_data
op_data = op_data & "<td>" & member & "</td>"
next
op_data = op_data & "</tr>"
next
op_data = op_data & "</table>"
Finally the processed data can be displayed: output_area.innerHTML = op_data
As mentioned before, VBScript only supports on error resume next and so any errors must be handled at the end of the subroutine: If (Err.Number <> 0) Then
output_area.innerHTML = "Unable to obtain stock quotes"
End If
End Sub
</script>
The Body SectionThe body section is used to define the GUI (Graphical User Interface) and uses HTML to do this, as well as some of the subroutines defined in the script section: <body>
<table>
<tr><th>Company</th><th>Additional Fields</th>
<tr><td valign=top>
<script language="VBScript">
company_check_list
</script>
</td><td valign=top>
<script language="VBScript">
field_check_list
</script>
</td></tr></table>
<input type="button" value="Get Stock Quotes"
id="run_vbscript_button" onclick="run_vbscript_button_click">
<div id=output_area></div>
</body>
ConclusionIf the three sections are stored in a text file with a .hta extension (for example yahoo.hta) then the HTA can be run by double clicking on its icon in Windows Explorer; and this simple, but useful, application will be available for use on any Windows pc to which it is copied.
The copyright of the article A Windows HTA Tutorial in Computer Programming Tutorials is owned by Mark Alexander Bain. Permission to republish A Windows HTA Tutorial in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|