Page 1 of 1
How to make IE give focus without complaining about errors?
Posted: Mon Oct 22, 2007 10:36 pm
by JAB Creations
I've tried the following though IE keeps complaining about errors (while still executing the script). I'd like IE to give an anchor focus.
My default guess is the following...
Code: Select all
document.getElementById('an_element').focus();
The script no matter how I modify it (document.all for example) always returns an error in IE 5.0, 5.5, 6.0, and 7.0 for all my other attempts (which aren't nearly as clean and direct as my initial method). So how can I give an anchor with an ID focus in IE without getting any JavaScript errors?

Posted: Tue Oct 23, 2007 4:23 am
by WaldoMonster
I don't know why your example doesn't work.
I have successfully used focus when using a complete path.
Code: Select all
<form action="myform.php" name="loginform" id="loginform">
<input type="text" name="username">
</form>
<script type="text/javascript">
document.loginform.username.focus();
</script>
Posted: Tue Oct 23, 2007 11:06 am
by califdon
Can you get a definitive error message or number? If IE does execute the focus shift, it doesn't seem that it should throw an error. Could the error be referring to something else?
Posted: Tue Oct 23, 2007 12:15 pm
by JAB Creations
document.getElementByID(...) is not an object.
Strange the issue disappeared after numerous refreshing? Stupid IE...

Thanks for the replies.
Posted: Tue Oct 23, 2007 12:28 pm
by Zoxive
JAB Creations wrote:document.getElementByID(...) is not an object.
Strange the issue disappeared after numerous refreshing? Stupid IE...

Thanks for the replies.
Are you waiting until the Dom/Page is loaded?
Posted: Tue Oct 23, 2007 12:30 pm
by JAB Creations
This has been occurring on my localhost. I don't usually have to wait too long for pages to load.

I think it may have had something to do with a DHTML error though this thread should be considered answered I suppose? Thanks!
Posted: Tue Oct 23, 2007 12:38 pm
by Zoxive
It would still happen on localhost, because the JavaScript is trying to execute immediately.
If your using something like mootools you could use Domready.
http://demos.mootools.net/DomReadyVS.Load
Posted: Tue Oct 23, 2007 12:39 pm
by s.dot
Put your javascript at the end of the page, before the </body>.
Posted: Tue Oct 23, 2007 12:43 pm
by JAB Creations
Thanks though I figured out the issue was related to a DHTML/jQuery script that I've posted in a separate thread
here which was my bad for overlooking. Thank you for your replies.