Update URL parameter with javascript, without ruining the history?
I'm looking for a way to update the url in the status bar.. I hav开发者_JS百科e a gallery, and when you click your way through the gallery I want the image ID to show up in the URL, so that the user can link directly to the image..
I've read about using hash and so. but as far as I've tried it, that "ruins" the history. If I click the back-button in my browser the previous image would be shown.
Is it possible to add or update a URL parameter, without ruining the history?
Thanks in advance
Use location.replace
to replace the current location:
Replace the current document with the one at the provided URL. The difference from the
assign()
method is that after usingreplace()
the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.
Do it simply this way, when switching to images, add a hash to the url, for example:
location+='#image-'+image_id
your location will become
http://example.org/images/#image-3
instead of the initial
http://example.org/images/
and onload, check if location.hash
is not empty, and matches with ^image-(\d+)$
(regular expression pattern), if it matches, do the usual thing you'd have done if a user clicks on image with id (\d+)
.
To preserve history, use reallysimplehistory.
精彩评论