PDA

View Full Version : adding focus to login form with javascript- help with errors


knot2afrayed
February 26th, 2006, 00:41
Hello, I am trying to add some javascript but am getting error of no object or object is null.

I have been trying to make it possible to add focus to the login username input, placing the cursor there so users can type in their username without having to click the username input box with their mouse.

Initially, I added to the body tag in my template index file
<body onLoad="document.login.username.focus();">


Which at first glance, worked fine. Load the page and the cursor is already blinking on the login form. But, after a user logs in, or goes to a page without the log in form, a javascript error shows up. Everything seems to work ok, but I'd like to get rid of the error if possible. When using IE, the error shows in the left bottom corner..and this is a bit unsightly. I've yet to find a way to make it work. I tried making a simple javascript function in the head section

<script type="text/javascript">
<!--
function addcursor ()
{
if (document.login) {
document.login.username.focus();
}
}
//-->
</script>
along with
<body onLoad="addcursor()">

Again, the focus function works, but error still results.
I want to know is there a way to check if object exists and if it does not avoid the error resulting.
Or do you have other suggestions on how this might be done?

Thanks (in advanced),

k2a

lordmerlin
February 26th, 2006, 02:52
I'm no JS guru, but what about doing an if statement?



<script type="text/javascript">
<!--
function addcursor ()
{

if(document.login.username.value=='') {
return true
} else {
document.login.username.focus();
}
}
//-->
</script>

knot2afrayed
February 26th, 2006, 09:22
Thanks for the suggestion,
but it didn't seem to work.
Same error.
There is no error after logging in actually if I use the code I mentioned above. It is only if I change to a page that doesn't have the login module.
I guess I could have the login module on every page.
I'll keep looking for better solutions though.

:)