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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default Focus On Button

Status
Not open for further replies.

markronz

IS-IT--Management
Mar 20, 2007
93
US
Hello everyone-
I am trying using vbscript, and am creating an IE window with it. So far this is the code that I have appearing in the window:

<p>Assist Complete. <i>(You may now close this window.)</i></p>

<p><BUTTON TYPE=BUTTON NAME="Close" TABINDEX=0 onClick="self.close()">Close </BUTTON></p>

All I would like to do is set it so that the focus in the new window is on the button so that the user can just hit Enter to close the window. Does anyone know how to control where the "focus" is? Or any other way to assign the Enter key to that button?

Thanks!
-Mark
 
I believe you have to use the onload event from the body of the page to set the focus.

something like:
Code:
<body Onload="buttonname.focus();">


----------------------------------
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.
 
Sorry, let me post a bit of my code to see if that helps at all. The focus() may work. I'm just not sure where to stick it. My code is a VBScript that launches an IE window.

Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Visible = True
objExplorer.ToolBar = False
objExplorer.StatusBar = False
objExplorer.Navigate "about:blank"
objExplorer.Document.Body.InnerHTML = "<p>Assist Complete. <i>(You may now close this window.)</i></p><p><BUTTON TYPE=BUTTON NAME=""Close"" TABINDEX=0 onClick=""window.close()"">Close </BUTTON></p><body Onload=""Close"".focus();>"



So when I place the <body Onload=""Close"".focus();> line at the end there it doesnt work. I apologize if this is out of your area because it has to be passed as a String through the VBScript. So it kind of has to be all in one line... Let me know if that doesn't make sense, or if you need more information.

Thanks
-Mark
 
Quite. Time to start finding out what HTML really is. I suggest this tutorial to begin with:
So, what is wrong.

1. Body, as name would imply, is the tag where the body of your page is. It will require an opening tag and a closing tag and it should surround all the data displayed on your page.

2. Let's review a few things about VB and html attributes. It is correct that all HTML attributes should be in quotes. That means that the attribute is in quotes, not that attribute includes quotes. You have two double quotes, because that is the way to escape double quotes in VB, which would mean the end of the string rather than outputting quotes. So, if you have name=""Close"", you have double quotes because of VB, meaning that the code outputs name="Close", which means that the value for name is Close. No quotes in value of the attribute. Value is quoted, but quotes are not part of the value. So, returning to our body tag, which you decided to not do like vacunita suggested. Let's review. He says:
Code:
<body Onload="buttonname.focus();">
And you do:
Code:
<body Onload=""Close"".focus();>
As you can see, he puts double quotes around the whole onload attribute. You don't. So, let's fix that.
Code:
<body onload="""Close"".focus();">
Now, we said that the attribute value does not include quotes, so your close is wrong there. Let's switch it up to:
Code:
<body onload="Close.focus();">
Hey, this would work if you had your body on correctly, so let's do that too:
Code:
objExplorer.Document.Body.InnerHTML = "<body onload="Close.focus();"><p>Assist Complete. <em>(You may now close this window.)</em></p><p><button type=""button"" name=""Close"" tabindex=""0"" onclick=""window.close()"">Close</button></p></body>"
This will almost work. It will give you a parse error, because we forgot to escape the quotes. So, let's escape them now!
Code:
objExplorer.Document.Body.InnerHTML = "<body onload=""Close.focus();""><p>Assist Complete. <em>(You may now close this window.)</em></p><p><button type=""button"" name=""Close"" tabindex=""0"" onclick=""window.close()"">Close</button></p></body>"
Voila, this one is done correctly. Now, in all honesty, could you really not have done this all by yourself?
 
Sorry, I do not think that I could have done that by myself at the time. I had been staring at the code all day, so for that I apologize. HTML isn't my strong suit. However, I still could not get it to work with the code above. Since then my code has changed slightly. So we can pretty much ignore all the VBScript stuff. Below is just some normal HTML code. I tried just creating a normal html file with this code but it correctly focus. All I want to do is have the focus show up on the button on the page but it doesn't work as written below:

Code:
<html><head>
<script language="vbscript">Dim Button : Button = 0</script>
</head><body onload="Close.focus();"><p>Assist Complete. <i>(You may now close this window.)</i></p>
<p><BUTTON TYPE=BUTTON NAME="Close" onClick="Button = 1">Close</BUTTON></p></body></html>
Any ideas what I'm doing wrong?

Thanks
-Mark
 
Give the button an id instead of a name. Call it something other than close too, that could be the problem.

Code:
<button id="closeButton".....

AND

<body onload="document.getElementById('closeButton').focus()">

Not sure how vbScript does inner quotes, I think it's "", so if the single quotes doesn't work, use "" in place of each single quote.


[monkey][snake] <.
 
This code will set the focus to the button

<html>
<head>
<title>Untitled</title>

</head>
<body onload="Close.focus();">
<p>Assist Complete. <i>(You may now close this window.)</i></p>
<p><BUTTON TYPE=BUTTON NAME="Close" onClick="Button = 1">Close</BUTTON></p>

</body>
</html>
but you have to hit the space bar to get it to actually click the button.

Paul
 
Or maybe not. In my editor(1st Page 2000) it sets the focus to the button, but when viewing it in my browser, it does not seem to work. Sorry if it's a false positive.

Paul
 
This worked in IE 6
It also set the focus in FF by did not close the window.

<html>
<head>
<title>Untitled</title>

</head>

<body onload="document.getElementById(1).focus();">
<p>Assist Complete. <i>(You may now close this window.)</i></p>
<p><input type = "button" id = "1" value = "Close" NAME="topbutton" onClick="window.close()"></p>

</body>

</html>

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top