最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.location.hash issue in IE7 - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Dec 6, 2013 at 2:38 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Jun 1, 2009 at 13:39 Vladimir KadalashviliVladimir Kadalashvili 7572 gold badges5 silver badges17 bronze badges 3
  • What do you mean with 'using document object'? You should use window.location as suggested in the answers - document.location is Gecko-specific! – Christoph Commented Jun 1, 2009 at 13:54
  • I used both of them - and they don't work – Vladimir Kadalashvili Commented Jun 1, 2009 at 14:24
  • Vladimir, works for me in IE7/XP ... – James Commented Jun 1, 2009 at 15:50
Add a comment  | 

4 Answers 4

Reset to default 6

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

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>

Have you tried changing just location.hash?

window.location.hash = "#" + hashName;

There is also a problem i came across

http://artur.ejsmont.org/blog/content/window-location-hash-difference-in-ff3-and-opera

发布评论

评论列表(0)

  1. 暂无评论