How to remove the hash from [removed] (URL) with JavaScript without page refresh?

前端 未结 16 3449
无人及你
无人及你 2020-11-22 02:53

I have URL like: http://example.com#something, how do I remove #something, without causing the page to refresh?

I attempted the following

16条回答
  •  生来不讨喜
    2020-11-22 03:04

    This will remove the trailing hash as well. eg: http://test.com/123#abc -> http://test.com/123

    if(window.history.pushState) {
        window.history.pushState('', '/', window.location.pathname)
    } else {
        window.location.hash = '';
    }
    

提交回复
热议问题