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

后端 未结 14 2257
忘掉有多难
忘掉有多难 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

    Facebook's photo gallery does this using a #hash in the URL. Here are some example URLs:

    Before clicking 'next':

    /photo.php?fbid=496429237507&set=a.218088072507.133423.681812507&pid=5887027&id=681812507
    

    After clicking 'next':

    /photo.php?fbid=496429237507&set=a.218088072507.133423.681812507&pid=5887027&id=681812507#!/photo.php?fbid=496435457507&set=a.218088072507.133423.681812507&pid=5887085&id=681812507
    

    Note the hash-bang (#!) immediately followed by the new URL.

提交回复
热议问题