Question

I am trying to learn the basics of HTA but I am running into a problem.

I want a HTA GUI with a text box that I can add my own VBScript to. When I press enter in the text box internet explorer opens and prompts me to open/save the HTA file.

What is causing this to happen and how can I stop it? I am using Windows 8 and have IE10.

<html>
<head>
<HTA:APPLICATION
  APPLICATIONNAME="Simple HTA"
SINGLEINSTANCE = "Yes"
  SYSMENU="yes">
<title>Simple HTA</title>
<style type="text/css">
  body {background-color:lightsteelblue;}
  p {font:bold 18px arial;}
</style>

<script language="VBScript">
sub checkEnter
  With document.parentWindow.event
    if .keycode = 13 then
      Validate()
    End if
  End With
End sub
sub validate()
x=msgbox("success")
End sub
</script>

</head>
<body>
<form>
<input type="text" onkeydown=checkEnter size=60 id="request" value="" />
<input type="button" name="btnStart" value="Start" onClick="Validate()" />
</form>
</body>
</html>
Was it helpful?

Solution

This happens, because "a little IE inside HTA" submits the form when pressing ENTER on a text input. There's a couple of ways to fix this:

If you don't need the form, just remove it, or replace it with a div.

Use a function as checkEnter and set:

event.cancelBubble = true
event.returnValue = false
return false

OTHER TIPS

I don't know if this example can help you or not but give it a try :)

<html>
<head>
<HTA:APPLICATION
APPLICATIONNAME="Search on Google with Internet Explorer"
BORDER="THIN"
BORDERSTYLE="NORMAL"
ICON="magnify.exe"
INNERBORDER="NO"
MAXIMIZEBUTTON="NO"
MINIMIZEBUTTON="NO"
SCROLL="NO"
SELECTION="NO"
SYSMENU="YES"
SINGLEINSTANCE="YES"/>
<META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES">
<script language="VBScript">
Option Explicit
Dim Titre
Titre = "Search on Google with Internet Explorer"
Self.document.title = Titre
Sub window_onload()
    CALL CenterWindow(300,180)
    Self.document.bgColor = "Orange"
End Sub
Sub RunProgram()
    Dim X,Y,URL,objShell,Param,MaCmd
    X = text1.value
    Y = text2.value
    Param = "#q="& X & "+" & Y &""
    URL = "www.google.com"& Param
    Set objShell = CreateObject("Wscript.Shell")
    msgbox("iexplore.exe " & DblQuote(URL)),VbInformation,Titre
    MaCmd = "iexplore.exe "& DblQuote(URL) &""
    objShell.Run(MaCmd)
End Sub
'***************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'***************************************************
'Position Windows
Sub CenterWindow(x,y)
    Dim iLeft,itop
    window.resizeTo x,y
    iLeft = window.screen.availWidth/2 - x/2
    itop = window.screen.availHeight/2 - y/2
    window.moveTo ileft, itop
End Sub
</script>
</head>
<body><center>
Text1 : <input type="text" id="text1" Name="text1" value="Hackoo"><br>
Text2 : <input type="text" id="text2" Name="text2" value="Vbscript+HTA"><br><br>
<input type="submit" Value="Search on Google" onclick="RunProgram()"></center>
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top