4

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.

2 Answers 2

5
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

Sign up to request clarification or add additional context in comments.

3 Comments

+1 - I didn't know about using the .search part of .location
This reloads the page. i only want to change the id for refresh when the user initiates, not for an instant refresh.
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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.