Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IE, javascript, moving to anchors, and setting focus

 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like for the browser window to scroll to whatever form element to which I'll set the focus (i.e. to the last clicked button). Firefox automatically moves it window, but IE does not.

Currently I have this:



which moves to the correct anchor, but doesn't set the focus to the element.

Anyone know how to do this in IE?

TIA.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you give the same code after it is generated after the page has been loaded with the view source. It will make it easier to see what is going on.

Also please check the checkbox that says disable smiles in the post!

Eric
 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the smiley

 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually,
whatever you have in "else" condition, should work for ie as well as for anything else.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what this code is for:

var anchor = '#newcustomerAnchor';
alert(anchor);
window.location.href=anchor;

I would just do what Yuriy said and use the else statement and get rid of the rest of the code.

Eric
 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The else part will set the focus to the element. However, the browser doesn't scroll the page to this element. I need to scroll to the focused element. That is what the anchor code is used.
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
starting with ie4, you can do

"scrollIntoView(true)" on almost any element.

For example document.getElementById("myid").scrollIntoView(true);
document.form[0].element[0].scrollIntoView(true);

here is a doc:
scrollIntoView MSDN information
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I tried what you doing in IE, (I mean window.location.href ="#test" ) , and it seems to be working fine,
if you do one of the following:
1. You make this call using onload event handler
2. you make this call after the "#test" link is defined

[ November 16, 2004: Message edited by: Yuriy Fuksenko ]
[ November 16, 2004: Message edited by: Yuriy Fuksenko ]
 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to get either of those to do both of the things that I need (ie scrolling and focusing). Fortunately, my boss has said that it's ok to just use anchor tags and move to them.

Thanks for your comments.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code I posted:

You need to call the function with the onload event handler in order to allow the whole page to load.



Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic