How to make IE give focus without complaining about errors?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

How to make IE give focus without complaining about errors?

Post 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? :roll:
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post 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>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post 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?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

document.getElementByID(...) is not an object.
Strange the issue disappeared after numerous refreshing? Stupid IE... :roll: Thanks for the replies.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

JAB Creations wrote:
document.getElementByID(...) is not an object.
Strange the issue disappeared after numerous refreshing? Stupid IE... :roll: Thanks for the replies.
Are you waiting until the Dom/Page is loaded?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

This has been occurring on my localhost. I don't usually have to wait too long for pages to load. :wink: I think it may have had something to do with a DHTML error though this thread should be considered answered I suppose? Thanks!
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

It would still happen on localhost, because the JavaScript is trying to execute immediately.

Code: Select all

<body onload="doFocus();">
If your using something like mootools you could use Domready.

http://demos.mootools.net/DomReadyVS.Load
Last edited by Zoxive on Tue Oct 23, 2007 12:43 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Put your javascript at the end of the page, before the </body>.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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.
Post Reply