View Full Version : focus onload

01-15-2004, 09:16 AM
for to give the focus to the first control I have this code :

<script type="text/javascript" language="javascript">function SetInitialFocus(){ document.frm['t_Login'].focus();}window.onload = SetInitialFocus;</script>

<form id="frm">
<input type="text" " id="t_Login" />

is there any way to write all directly in the control itself, xsomething like

<input type="text" " id="t_Login" onload="SetInitialFocus" />


thank you

01-15-2004, 09:21 AM
No. Why don't you want that window.onload thing?
Maybe you want it shorter.

<script type="text/javascript">
onload = function(){document.frm['t_Login'].focus()}

01-15-2004, 09:50 AM
ok shorter is allready good

thank you !

01-15-2004, 10:23 AM
<input type="text" " id="t_Login" onload="SetInitialFocus" />

Anyway, that seems not correct to me, as onload is an excusive window's event handler.

window.onload = function (or onload = function , which is the same) and
<body onload="function"> are correct.

Probably <tagOtherThanBody onload> will fire exactly like <body onload> but I am affraid that if there will be 2 or more of these, the code will be useless, if not even will bring javascript errors...

01-15-2004, 11:01 AM
then no possibility to write the setFocus directly into the first control without any external script tag ?

01-15-2004, 12:00 PM
without any external script tag

but why? :confused:

It is true that you may fire a function using something like

"javascript:a_function()" but you need by all means an eventhandler to fire it this way....

01-15-2004, 12:16 PM
you can write onkeypress:retrun false;

then why not



01-15-2004, 12:43 PM
Because form elements don't load anything. Images, frames, documents - they all load, and that's why they have load events.

You could try the readyState property - that might return a value, in IE at least.

01-15-2004, 12:46 PM
angiras - The code provided by glenngv will do exactly what you would like for it to do... You can set focus on an object...


01-15-2004, 02:11 PM
yes The code provided by glenngv works fine ! no prob !