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? Thanks
+2
A:
Have you tried changing just location.hash
?
window.location.hash = "#" + hashName;
Gumbo
2009-06-01 13:42:50
I have tried this, but it also doesn't work
Vladimir Kadalashvili
2009-06-01 13:44:03
not familiar with this. looks good, though. *goes to test...*
Jonathan Fingland
2009-06-01 13:45:20
works in firefox 3
Jonathan Fingland
2009-06-01 13:54:22
doesn't work in IE7, though.
Jonathan Fingland
2009-06-01 14:08:50
+3
A:
IE and most other browsers will scroll to an anchor with anchor.focus(), or to any element with an id with element.scrollIntoView(true)
kennebec
2009-06-01 15:17:23
+3
A:
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>
Pim Jager
2009-06-01 15:42:42
A:
There is also a problem i came across
http://artur.ejsmont.org/blog/content/window-location-hash-difference-in-ff3-and-opera
Art79
2010-08-19 15:01:41