Change the URL in the browser without loading the new page using JavaScript

后端 未结 14 2160
忘掉有多难
忘掉有多难 2020-11-22 02:02

How would I have a JavaScript action that may have some effects on the current page but would also change the URL in the browser so if the user hits reload or bookmark, then

14条回答
  •  有刺的猬
    2020-11-22 02:36

    A more simple answer i present,

    window.history.pushState(null, null, "/abc")
    

    this will add /abc after the domain name in the browser URL. Just copy this code and paste it in the browser console and see the URL changing to "https://stackoverflow.com/abc"

提交回复
热议问题