How can I remove the location hash without causing the page to scroll?

后端 未结 8 1570
情深已故
情深已故 2020-12-04 08:18

Is it possible to remove the hash from window.location without causing the page to jump-scroll to the top? I need to be able to modify the hash without causing

8条回答
  •  情深已故
    2020-12-04 08:52

    I use the following on a few sites, NO PAGE JUMPS!

    Nice clean address bar for HTML5 friendly browsers, and just a # for older browsers.

    $('#logo a').click(function(e){
        window.location.hash = ''; // for older browsers, leaves a # behind
        history.pushState('', document.title, window.location.pathname); // nice and clean
        e.preventDefault(); // no page reload
    });
    

提交回复
热议问题