Rich-text Reply

How to append a query without a redirect?

AndiK 06-28-16
Accepted Solution

How to append a query without a redirect?

Hello,

 

is there any way to add a query like "?variant=b" to an URL without doing a redirect?

In the code samples there is only a solution with a redirect. (http://developers.optimizely.com/javascript/code-samples/index.html#helpers-add-query-param).

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.

 

Thanks

Level 2

JasonDahlin 06-28-16
 

Re: How to append a query without a redirect?

[ Edited ]

hi @AndiK,

 

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).

 

--Jason Dahlin
Analytics and Testing Guru Smiley Happy
AndiK 06-28-16
 

Re: How to append a query without a redirect?

[ Edited ]

hi @JasonDahlin

thanks for the code. It works fine.

Level 2