How can I remove the query string from the url after the page loads?

前端 未结 10 1113
栀梦
栀梦 2020-12-16 10:03

I have a URL with a long query string attached to it. After the page loads, I do not require the query string. So I want to remove the query string from the address bar with

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 10:35

    That is achievable in modern browsers using the history api, but probably isn't the best solution to your problem.

    history.replaceState({}, 'some title', '/');
    

    It sounds like you would be better off processing the data and then redirecting to the homepage instead of returning an HTML document directly.

    Since you aren't wanting to keep the URL around, it won't be useful for bookmarking, so it is quite likely you would be better off making a POST request.

    This suggests that you should be using the POST-Redirect-GET pattern.

提交回复
热议问题