How do I navigate to hashtag after page load?

后端 未结 5 1090
忘掉有多难
忘掉有多难 2021-02-01 07:38

I want to do the inverse of what I\'ve been finding so far. I\'m setting a lot of heights with js and I want to navigate to the hashtag in the url after the page has loaded. I\'

5条回答
  •  醉酒成梦
    2021-02-01 08:22

    For some reason both MS Edge 42 and IE 11 will not scroll to the new bookmark for me, even when doing a window.location.reload(true) after setting the new bookmark. So I came up with this solution: insert this script on the page you're loading (requires jquery)

    $(document).ready(function() {
     var hash = window.location.hash;
     if (hash) {
      var elem = document.getElementById(hash.substring(1));
      if (elem) {
       elem.scrollIntoView();
      }
     }
    });
    

提交回复
热议问题