Note: I moved this from Stack Overflow to Code Review by suggestion of Tacoshy who added: "Instead of using an onclick attribute, why not simply use a single function with either a switch statement or using the data-attribtue to provide information?"
For some background, I'm someone who normally works in VBScript and Batch files, so the whole HTML space is new to me and there are many aspects that are foreign to me. If someone could give a some simple example of what he was referring to, I'd appreciate it as I thought the OnClick attribute would be the only way to take action in an HTA.
My original post from Stack Overflow:
I have about 300 links I want to put in a reference HTA file for new people to refer to. For each link, I want the user to be able to either copy the URL or launch the site for each. The problem I'm running into is I can't find a way for the HTML code to set the value of a generic variable, use that in the input field then pass that through to generic VBScript subroutines that use those variables in their actions. Instead, I'm having to create a sub for each link, where a specifically named variable is set by the field's HTML code then passed to a subroutine that only uses that specific variable. So for 300 links, I'm ending up with 300 variables I need to set and 600 subroutines, one for link copy and one for link launch, for each and every URL. Is there any way to make this more efficient and more manageable? I was hoping I could set a variable to the URL, use that variable in the input field and then pass that variable to the VBScript subs as a generic variable name so I can have just one sub per action that any link to it can utilize by feeding it that generically named variable. Any help would be appreciated.
Here's some sample code to show you what I've come up with so far to just get this to work:
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Reference Websites Test</title>
<HTA:APPLICATION
APPLICATIONNAME="Reference Websites Test
Caption="Yes"
MaximizeButton="Yes"
MinimizeButton="Yes"
SCROLL="Yes"
SINGLEINSTANCE="yes"
WINDOWSTATE="Normal"
BORDER="Normal"
Icon=C:\Windows\System32\Magnify.exe
<font face=Arial>
</head>
<script language="VBScript">
dim WebsiteX,YahooSite,AOLSite,DuckDuckGoSite
YahooSite="https://www.yahoo.com"
AOLSite="https://www.AOL.com"
DuckDuckGoSite="https://www.duckduckgo.com"
Sub YahooOpen
WebsiteX=YahooSite
OpenSite
End Sub
Sub YahooCopy()
document.parentwindow.clipboardData.SetData "text", YahooURL.value
MsgBox "Link Copied to the Clipboard",vbInformation,Title
End Sub
Sub AOLOpen
WebsiteX=AOLSite
OpenSite
End Sub
Sub AOLCopy()
document.parentwindow.clipboardData.SetData "text", AOLURL.value
MsgBox "Link Copied to the Clipboard",vbInformation,Title
End Sub
Sub DuckDuckGoOpen
WebsiteX=DuckDuckGoSite
OpenSite
End Sub
Sub DuckDuckGoCopy()
document.parentwindow.clipboardData.SetData "text", DuckDuckGoURL.value
MsgBox "Link Copied to the Clipboard",vbInformation,Title
End Sub
Sub OpenSite
Set browobj = CreateObject("Wscript.Shell")
browobj.Run "chrome.exe -new-window -start-maximized -url " & WebsiteX
End Sub
</script>
<body>
<font face=Arial>
<font color=FFFF23>
<body bgcolor=#1D4D7C>
<font size="-1">
<text><b>This site is not used much anymore except for webmail.</b></text>
<br>
<!-- The Yahoo text field -->
<input type="text" value="https://www.yahoo.com" id="YahooURL" size="50" readonly="readonly">
<!-- The button used to copy the text -->
<input type="button" name="ButtonCopy" value="Copy Link" onclick="YahooCopy">
<!-- The button used to open browser to that page -->
<button onclick="YahooOpen">Click to Open Site</button>
<br>
<br>
<text><b>This site is old.</b></text>
<br>
<!-- The AOL text field -->
<input type="text" value="https://www.aol.com" id="AOLURL" size="50" readonly="readonly">
<!-- The button used to copy the text -->
<input type="button" name="ButtonCopy" value="Copy Link" onclick="AOLCopy">
<!-- The button used to open browser to that page -->
<button onclick="AOLOpen">Click to Open Site</button>
<br>
<br>
<text><b>This site is newer and spies less than Google.</b></text>
<br>
<!-- The DuckDuckGo text field -->
<input type="text" value="https://www.duckduckgo.com" id="DuckDuckGoURL" size="50" readonly="readonly">
<!-- The button used to copy the text -->
<input type="button" name="ButtonCopy" value="Copy Link" onclick="DuckDuckGoCopy">
<!-- The button used to open browser to that page -->
<button onclick="DuckDuckGoOpen">Click to Open Site</button>
</font>
</body>
1 Answer 1
I recommend that you put the data in a separate text file and then build the page dynamically.
A separate file for the data allows the 300+ entries to be in a simple list format (i.e. no html) that's easy to maintain, even by a non-programmer. It eliminates the need to touch the HTA code every time there's an update to the data.
Below is an example of how this can be done. Note that this example code generates a button for the user to open each URL, but the code could easily be modified to make the URL a clickable link.
Sitelist.txt
https://www.yahoo.com|This site is not used much anymore except for webmail
https://www.AOL.com|This site is old
https://www.duckduckgo.com|This site is newer and spies less than Google
Sitelist.hta
<!DOCTYPE html>
<html>
<head>
<title>Reference Websites</title>
<meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9">
<HTA:APPLICATION
Icon=Magnify.exe
>
<script language="VBScript">
w = 1024
h = 600
window.resizeTo w, h
window.moveTo (screen.availWidth - w)/2, (screen.availHeight - h)/2
Set oWSH = CreateObject("Wscript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
dataFile = ".\SiteList.txt"
Data = oFSO.OpenTextFile(dataFile).ReadAll
aData = Split(Data,VBCRLF)
Sub CopyURL(URL)
document.parentwindow.clipboardData.SetData "text", URL
MsgBox URL,,"On Clipboard:"
End Sub
Sub GotoURL(URL)
oWSH.Run "chrome.exe -new-window -start-maximized -url " & URL
End Sub
</script>
<style>
body {background-color:#B8D8E9; font-family:Segoe UI; font-size:11pt}
input[type="button"] {font-family:Segoe UI Emoji; font-size:11pt}
.url { font-family:Comic Sans MS; color:blue }
.info { color:gray }
</style>
</head>
<body>
<script language="VBScript">
For i = 0 To UBound(aData)
If InStr(aData(i),"|")>0 Then
aLine = Split(aData(i),"|")
h01 = "<input type=button value=π onclick=CopyURL(""" & aLine(0) & """)> "
h02 = "<input type=button value=π onclick=GotoURL(""" & aLine(0) & """)> "
h03 = "<span class=url>" & aLine(0) & "</span> "
h04 = "<span class=info>(" & aLine(1) & ")</span><br><br>"
document.write (h01 & h02 & h03 & h04)
End If
Next
</script>
</body>
</html>