Reputation: 195
So here is what i am wondering about boys and girls - first of all i am a complete noob to this, LITERALLY just starting so don't go too hard on me. I have a bunch of code i wrote, to make a .HTA application:
> <html> <head>
>
> <script type="text/vbscript">
>
> Dim objShell Sub Button1_OnClick()
>
> if box2.checked AND box1.checked then
>
> Set objShell = CreateObject( "WScript.Shell" )
> objShell.Run("""%programfiles(x86)%\Mozilla Firefox\firefox.exe""")
> Set objShell = Nothing
>
> Set objShell = CreateObject( "WScript.Shell" ) objShell.Run("cmd.exe")
> Set objShell = Nothing
>
> elseif box1.checked then
>
> Set objShell = CreateObject( "WScript.Shell" ) objShell.Run("cmd.exe")
> Set objShell = Nothing
>
> Elseif box2.checked then
>
> Set objShell = CreateObject( "WScript.Shell" )
> objShell.Run("""%programfiles(x86)%\Mozilla Firefox\firefox.exe""")
> Set objShell = Nothing
>
>
> End If End Sub
>
>
> </script> </head> <body> <font face=Calibri> Check the program you
> would like to run! <br> Available programs to run for now: <br> <input
> type="checkbox" name="box1">CMD <br> <input type="checkbox"
> name="box2">Mozilla <br> <i>Choose which program(s) you'd like to run.
> It is possible to run multiple programs at one time!</i></font><br>
> <input type="button" name="btn1" onclick="Button1_OnClick"
> value="Submit"><br> <div id="error"></div>
>
>
>
> </body> </html>
This works perfectly as it is supposed to, when i check both programs, both of them will run, when i check one of them, only one will run. But what if i have like 50 different programs on that list? I suppose there is a simplier way to write this than writing load of if/else/elseif statements for each program combination? As mentioned above im complete noob to this, maybe i simply haven't discovered an easier way yet... But that's also why i ask.
Upvotes: 0
Views: 1620
Reputation: 16950
You can use the elements attributes for your purpose.
Iterate over all the checkboxes, then start the process using its path stored in your custom attribute if it's checked. Done.
<html>
<head>
<script type="text/vbscript">
Dim objShell
Set objShell = CreateObject("WScript.Shell")
Sub StartProcesses
Dim Checkbox
For Each Checkbox In Document.getElementsByName("process")
If Checkbox.Checked Then
objShell.Run """" & Checkbox.getAttribute("path") & """"
End If
Next
End Sub
</script>
</head>
<body>
<font face=Calibri>
Check the program you would like to run! <br>
Available programs to run for now: <br>
<div id="ProcessList">
<input type="checkbox" name="process" path="cmd.exe">CMD <br>
<input type="checkbox" name="process" path="iexplore.exe">Internet Explorer <br>
<input type="checkbox" name="process" path="%programfiles(x86)%\Mozilla Firefox\firefox.exe">Firefox <br>
<input type="checkbox" name="process" path="calc.exe">Calculator <br>
<input type="checkbox" name="process" path="notepad.exe">Notepad <br>
</div>
<i>Choose which program(s) you'd like to run. It is possible to run multiple programs at one time!</i>
</font><br>
<input type="button" onclick="StartProcesses" value="Submit"><br>
<div id="error"></div>
</body>
</html>
Upvotes: 2