Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.location.hash issue in IE7

We have a javascript function that should "move" a page to a certain position using anchors. This function just does window.location.href = "#" + hashName. This works in FF, but not in IE. I tested this code using IE7 under Windows XP. I have tried using window.location.href, window.location.hash, window.location.replace and all these ways, but using document object. Does anyone know how to deal with this issue?

like image 299
Vladimir Kadalashvili Avatar asked Jun 01 '09 13:06

Vladimir Kadalashvili


3 Answers

IE and most other browsers will scroll to an anchor with anchor.focus(), or to any element with an id with element.scrollIntoView(true)

like image 88
kennebec Avatar answered Nov 03 '22 13:11

kennebec


I justed tested this in IE7 under Vista, maybe the issue only exsists in IE7 under XP? Because this works fine for me in IE7, Chrome and Firefox:

 window.location.hash = hashName;

If this really doesn't work then we could use scrollIntoView as Kennebec suggests.

 function scrollToAnchor(anchorName){
   //set the hash so people can bookmark
   window.location.hash = anchorName;
   //scroll the anchor into view
   document.getElementsByName(anchorName)[0].scrollIntoView(true);
 }

Use like this:

 <script type='text/javascript'>scrollIToAnchor('foo');</script>
 <a name='foo'></a>
 <p>I will be scrolled into view</p>
like image 42
Pim Jager Avatar answered Nov 03 '22 15:11

Pim Jager


Have you tried changing just location.hash?

window.location.hash = "#" + hashName;
like image 36
Gumbo Avatar answered Nov 03 '22 13:11

Gumbo