I am looking for a way to alter the get query string of an html request using javascript (jquery included) without refreshing the page. This query string would be carried over when refresh. For instance, http://thissite.site/index.html?id=123 would be the original url, then an event happens and id changes to 235. When the page is refreshed, the refreshed link would be http://thissite.site/index.html?id=235.
Add a comment
|
2 Answers
window.location.search = "?id=" + encodeURIComponent(new_value)
https://developer.mozilla.org/docs/DOM/window.location
https://developer.mozilla.org/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
3 Comments
Travis J
+1 - I didn't know about using the
.search part of .locationsteventnorris
This reloads the page. i only want to change the id for refresh when the user initiates, not for an instant refresh.
Blazemonger
Then use hashes instead, or make it a hyperlink. The browser won't let your code change the URL displayed without actually changing the page displayed; this would be a massive security hole and be exploited by phishers constantly.
With HTML 5 you can use the the following pushState function. More info found on How does pushState protect against potential content forgeries?.
I was looking for this as well and saw bandcamp using this for the navigation on http://bandcamp.com/discover