File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes IE, javascript, moving to anchors, and setting focus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "IE, javascript, moving to anchors, and setting focus" Watch "IE, javascript, moving to anchors, and setting focus" New topic
Author

IE, javascript, moving to anchors, and setting focus

Samuel Cox
Ranch Hand

Joined: Nov 16, 2004
Posts: 96

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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 16, 2004
Posts: 96

Sorry about the smiley

Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
Actually,
whatever you have in "else" condition, should work for ie as well as for anything else.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 16, 2004
Posts: 96

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

Joined: Feb 02, 2001
Posts: 413
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
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

Joined: Nov 16, 2004
Posts: 96

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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IE, javascript, moving to anchors, and setting focus