Search code examples
javascriptlocation-href

location.reload() with new query string


I want to simply reload a page with a new querystring as a user fills in settings on a form. I could use location.href and just redirect them to the same page with the new query string, but I want the user to be able to click back once, to return to the previous page. By using location.href, each time they change a setting, it adds to the browser history and they end up having to hit back as many times as they change settings to get back to the previous page.

I tried using location.assign() and that seemed to work in firefox when I followed it up with a location.reload() immediately afterwards, but it didn't work in chrome.

Is this possible to do?

TIA


Solution

  • Depending on what browsers you're trying to support you should look into history.replaceState

    So you'd do something like:

    history.replaceState(null, null, location.href + queryString);

    And of course you would fill in the null arguments accordingly if needed.