Click to See Complete Forum and Search --> : onload alternative
Heleen
09-24-2003, 02:32 AM
I am a total noob on Javascript. I need to focus on an input box but I can't do it in the body tag.
So usually it would be like
<body onLoad=self.focus();document.formname.inputfield.focus()>
How do I make it to focus after the page is loaded ?
Thanks in advance
96turnerri
09-24-2003, 03:35 AM
an easier method could be to use a bookmark
Heleen
09-24-2003, 03:37 AM
A bookmark ? Sorry I can't follow you here.
96turnerri
09-24-2003, 04:22 AM
if you bookmarked the item you wish the page to load on then make the link a bookmark link
<a name="box1"><input type="text" name="T1" size="20"></a>
<a href="http://www.WHATEVR.com/page3.htm#box1>Next Page</a>
96turnerri
09-24-2003, 04:22 AM
if you bookmarked the item you wish the page to load on then make the link a bookmark link
<a name="box1"><input type="text" name="T1" size="20"></a>
<a href="http://www.WHATEVR.com/page3.htm#box1>Next Page</a>
Heleen
09-24-2003, 04:30 AM
Maybe I wasn't clear enough. I have an input field that needs a FOCUS when the page is loaded so people dont have to use tabs or their mouse to fill in the form. Normally I would use
<body onLoad=self.focus();document.formname.inputfield.focus()>
but I can't now because a different asp file fills this the top part of the page.
96turnerri
09-24-2003, 04:54 AM
o right sorry so you want the cursor in the text box? i thought u wanted the page to open with the tb at the top lol so sorry, right on it
Heleen
09-24-2003, 05:03 AM
s'ok...it's still early :)
Charles
09-24-2003, 06:17 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>
<script type="text/javascript">
<!--
onload = function () {document.king.tut.focus()}
// -->
</script>
<form action="" name="king">
<div>
<label>King<input type="text" name="tut"></label>
<button type="submit">Submit</button>
</div>
</form>
AdamBrill
09-24-2003, 07:38 AM
Originally posted by Charles
<script type="text/javascript">
<!--
onload = function () {document.king.tut.focus()}
// -->
</script> However, if the ASP file is already generating an onload in the body tag, then this won't work. One of the two will get overwritten...
Gollum
09-24-2003, 08:27 AM
So you use a chain - this takes me back...
<script>
var nextOnload = window.onload;
function myOnload()
{
document.king.tut.focus();
this.nextOnload();
}
window.onload = myOnload;
</script>
AdamBrill
09-24-2003, 11:03 AM
Originally posted by Gollum
So you use a chain - this takes me back...
<script>
var nextOnload = window.onload;
function myOnload()
{
document.king.tut.focus();
this.nextOnload();
}
window.onload = myOnload;
</script>
That didn't work for me... But you gave me an idea, so here is what I came up with. ;) This code will NOT work if you put it in the head. Make sure you put it in the body:<script type="text/javascript">
var nextOnload = window.onload;
function myOnload(){
document.king.tut.focus();
this.nextOnload();
eval(nextOnload);
}
nextOnload = String(nextOnload).substring(String(nextOnload).indexOf('{')+1,String(nextOnload).lastIndexOf('}'));
onload=myOnload;
</script>
Heleen
09-25-2003, 03:36 AM
Thank you all :)
I'll give it a try now I am back at work.