Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set focus on a text field in HTA 1

Status
Not open for further replies.

Utokia

Technical User
Jan 29, 2011
4
BE
Hi,

Well, my first post on this forum.
Perhaps there is someone who can help me with this.
Why is the function "xFocus ()" not working.
I have already tried several ways to get it working.
I would like to have direct focus on the text field,
when the application starts.
Any help will be greatly appreciated.

____________________________________________________

Html>
<Head>


<SCRIPT language="JScript">
window.resizeTo(1,1);
window.moveTo(-1500,1);
</SCRIPT>
<hta:application
ID="oHTA"
applicationName="ASCII CODE"
caption="No"
windowstate="Normal"
/>
<SCRIPT language="JScript">
function position(iWidth,iHeight){
var iLeft = (window.screen.width-iWidth)/2;
var iTop = (window.screen.height-iHeight)/2;
window.moveTo(iLeft,0);
window.resizeTo(iWidth,iHeight);
try{
window.self.focus()
}
catch(ee){}
document.title = oHTA.applicationName
}
</SCRIPT>
<script type="text/javascript">

function xFocus() {

document.form1.login.focus();

}

</script>

<script type="text/javascript">
function displayunicode(e){
var unicode=e.keyCode? e.keyCode : e.charCode
alert (unicode)
}
</script>
</Head>
<Body onload="setTimeout('position(100,100)',0)"; "xFocus()">

<form name="form1">

<input type="text"

Style="Position:Relative;Width:45px;Height:20px"

Style="Background:aqua"

onkeyup="displayunicode(event); this.select()">
<form method="post">
<input type="button"
Style="width: 45px; height: 25px"
Style="background: Red"
Value="Close"

onclick="window.close()">
</form>
</body>
</html>

____________________________________________________

Kind regards
Utokia
 
Hi

Let us color the attribute values abit :
Code:
<Body onload=[highlight]"setTimeout('position(100,100)',0)"[/highlight]; [highlight]"xFocus()"[/highlight]>
Do you see it now ? The xFocus() call is in the air, not pertaining to any attribute, so it is just ignored.


Feherke.
 
Feherke is right on the money. Also since this is a Javascript related question, and not directly HTML, may I suggest posting in the forum216 for any further questions of this nature.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi Feherke and Vacunita

First of all, HTA is HTML application,
that's why i posted it in this sub forum.
The function "xFocus ()"is not empty, i believe.
This is the piece of code that runs when the body is loaded :
______________________________________________________
<script type="text/javascript">


function xFocus() {

document.form1.login.focus();

}

</script>
______________________________________________________
Here is an example with the same function,
paste it into notepad, save it with the extension .HTA and run it.
You will see that the text field gets the focus.
______________________________________________________

<Html>
<Head>


<title>ASCII CODE</title>
<hta:application
caption="No"
version="1.0"
windowstate="Normal"
/>

<script type="text/javascript">


function xFocus() {

document.form1.login.focus();

}

</script>

<script type="text/javascript">
function displayunicode(e){
var unicode=e.keyCode? e.keyCode : e.charCode
alert (unicode)
}
</script>
<Script Language="VBScript">
Sub Window_Onload
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")
For Each objItem in colItems
intHorizontal = objItem.ScreenWidth
Next
intLeft = (intHorizontal-16) / 2
window.resizeTo 100,100
window.moveTo intLeft, 0
End Sub
</Script>

</head>

<body onload="xFocus()">

<form name="form1">

<input type="text"


Style="Background:Yellow"

Style="Position:Relative;Width:45px;Height:20px"

name="login"

onkeyup="displayunicode(event); this.select()">
<form method="post">
<input type="button"

Style="width: 45px; height: 25px"

Style="background: Red"
Value="Close"

onclick="window.close()">
</form>
</body>
</html>

______________________________________________________

The only difference in this example is that the repositioning of the HTA window,
is called in a different way.
You can see that when the window of the HTA is opened,
it flashes before it takes the right position.
That's why I use the method of the first example,
so that the window of the HTA is opened without a flash.
But apparently this conflicts with the "xFocus ()" function.

Kind regards
Utokia
 
HTA is for the most part HTML, calling functions from events is Javascript

The point feherke was trying to make was that your call to xfocus() was not being executed because its outside the onLoad event.

Element Events are surrounded by quotes. Once you close the quote the event actions end. Anything inside the quotes is executed in the order it appears.

In your code the onLoad event ends after the call to setTimeout, once you close the quotes.

Code:
<Body onload="setTimeout('position(100,100)',0)[red]"[/red]; "xFocus()">

The subsequent call to xFocus is just taken as an odd value and ignored as a call to a function.

Include the call in the original set of quotes.
Code:
<Body onload="setTimeout('position(100,100)',0)[COLOR=red yellow];[/color] xFocus();[red]"[/red]>


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
<Body onload="setTimeout('position(100,100)',0)"; "xFocus()">

But i had tried that already,
and that does not work either.
If you would have tried it yourself, you would have noticed that.
 
Utokia, you need to step back and very carefully re-read feherke's and vacunita's last post. If you look closely at their posts, they're quoting your code and pointing out your mistake. Vacunita was even nice enough to show you how to do it correctly. Please try and copy and paste vacunita's second code block into your own code and see if it works. When it works, make sure you understand why it works.

The issue they've been trying to highlight is that javascript gets executed between the double quotes of html attributes, such as onload="" in your example. However, your code does not have the xfocus() call within the quotes, it has it outside. When javascript appears outside attribute values, it is ignored, since parser has no idea what to do with it. That is why your xfocus() is ignored, not because script itself is not working. It just never gets called.

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Both codes have issues like having one form inside another form, but the top form never ends. There's also the difference of the names. Your second code gives the text box a name so it can be accessed through document.forms.elementname or elementID. Third the fact your call to xfocus() was incorrectly placed.
Code:
<form name="form1">

[COLOR=#dedede #888888] <input type="text"

    Style="Position:Relative;Width:45px;Height:20px"

    Style="Background:aqua"
    
onkeyup="displayunicode(event); this.select()">[/color]
<form method="post">
    <input type="button"
    Style="width: 45px; height: 25px"
    Style="background: Red"
    Value="Close"
    
onclick="window.close()">
</form>

I strongly suggest your read on correct HTML and CSS coding, because your are going to run int many many difficulties if you don't understand the basic syntax.


Now here is a corrected code, that works because I've tested it in IE8.

Code:
<Html>
<Head>
<SCRIPT language="JScript">
    window.resizeTo(1,1);
    window.moveTo(-1500,1);
</SCRIPT>
<hta:application 
    ID="oHTA"
    applicationName="ASCII CODE"
    caption="No"
    windowstate="Normal"
/>
<SCRIPT language="JScript">
function position(iWidth,iHeight){
    var iLeft = (window.screen.width-iWidth)/2;
    var iTop = (window.screen.height-iHeight)/2;
    window.moveTo(iLeft,0);
    window.resizeTo(iWidth,iHeight);
    try{
        window.self.focus()
    }
    catch(ee){}
    document.title = oHTA.applicationName
}
</SCRIPT>
<script type="text/javascript">

    function xFocus() {
alert("Focusing");
    document.form1.login.focus();

}

</script>

<script type="text/javascript">
    function displayunicode(e){
    var unicode=e.keyCode? e.keyCode : e.charCode
    alert (unicode)
}
</script>
</Head>
<Body onload="setTimeout('position(100,100)',0); xFocus();">

<form name="form1">

<input type="text"

    Style="Position:Relative;Width:45px;Height:20px; Background:aqua;"
    
onkeyup="displayunicode(event); this.select();">
    <input type="button"
    Style="width: 45px; height: 25px; background: Red;"
    Value="Close" [red]name="login"[/red] onclick="window.close();">
</form>
</body>
</html>

The alert shows that its running the focus function on load.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top