Focusing HTML element immediately after loading the web page

Focusing (i-e putting cursor) appropriate control/HTML element in a webpage will enhance the user experience.

It should be done once after loading the page.

We can use the below javascript code for doing this ononloadĀ event of window.

This code should be placed inside the head tag.
<script type=”text/javascript”>
window.onload = function givefocus(){document.forms[0].elements[0].focus()}
</script>

Another way is we can do it on the onLoad event of body tag as below,

<body onLoad=”document.frmname.name.focus();”>
<form name=”frmname” id=”frmname” action=”” method=”post”>
<input type=”text” name=”name”>
</form>
</body>

Posted in General, Html