16

Is there a way to set the values of a querystring using javascript?

My page has a filter list that when clicked, it will alter the in-page results pane on the right hand side.

I'm trying to update to the querystring values of the url, so if the user leaves the page, then clicks the "back" buttons they'll be return to the last filter selection set.

For example:
Landing: foo.html
Click 1: foo.html?facets=bar
Click 2: foo.html?facets=bar|baz
Click 3: foo.html?facets=bar|baz|zap

Is this possible?

askedApr 5, 2011 at 1:32
rsturim's user avatar
2
  • 1
    Might be better achieved using a filter values cookie?CommentedApr 5, 2011 at 1:37
  • You might take a look atBackbone, especially it'sBackbone.Router class.CommentedApr 12, 2012 at 7:46

3 Answers3

25

You can useURLSearchParams. Just note that it isn't available for Internet Explorer 11.

// Get current URL partsconst path = window.location.pathname;const params = new URLSearchParams(window.location.search);const hash = window.location.hash;// Update query string valuesparams.set('numerical', 123);params.set('string', 'yummy');params.set('multiple', ['a', 'b', 'c']);params.set('foreignChar', 'é');// Encode URL// numerical=123&string=yummy&multiple=a%2Cb%2Cc&foreignChar=%C3%A9console.log(params.toString());// Update URLwindow.history.replaceState({}, '', `${path}?${params.toString()}${hash}`);
Zeke Lu's user avatar
Zeke Lu
7,5781 gold badge21 silver badges27 bronze badges
answeredJun 26, 2019 at 16:34
Dryden Williams's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

This is great but you should also includelocation.hash afterparams.toString
9

It is possible, but it will refresh the page.

document.location = "?facets=bar";

If you don't care about browser support, you can use the HTML5history.pushState.

answeredApr 5, 2011 at 1:38
Peter Olson's user avatar

2 Comments

BTW, now in 2016,almost all browsers supporthistory.pushState.
If you don't want to create a new history entry, usehistory.replaceState instead ofhistory.pushState. (morehere)
4

You can use Javascript to change the hash (the #hash-part of the URL), but changing the query string means you have to reload the page. So, no, what you want to do is not possible in that way.

An alternative is to use Javascript to change the hash, then check the hash on page load to change your results dynamically. You're looking for something likejQuery Address.

answeredApr 5, 2011 at 1:39
Kelly's user avatar

1 Comment

As shown in @Dryden Williams answer, the URL can be changed without reloading the page by using window.history.replaceState() or window.history.pushState(). This did not have full support when the question was originally asked but can be done in all major browsers now.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.