How to append a query without a redirect?
is there any way to add a query like "?variant=b" to an URL without doing a redirect?
The problem is when the reload of the page starts, the browser doesn't remember on which position it was before and shows you the top of the page.
Solved! Go to Solution.
In its simplest form, you can add "?a=b" to the URL by running this code.
window.history.pushState("","", window.location.href + "?a=b");
In the real world, you would want to make sure there isn't already a ? in the URL, make sure your parameters are being placed in front of any "#anchorName" portions of the URL, etc., but for illustration purposes, this is how you change the URL without causing the page to reload.
However, this does change is not picked up by the web server. So if your intention is to get a variation of the page to load due to the presence of that URL parameter, it won't work.
Note: This is not supported by older browsers. You'll want to do some browser feature support investigation to see if your user base uses browsers that support this method (e.g., if a lot of your users still use IE8, you probably don't want to do this).
Analytics and Testing Guru