Rich-text Reply

Remove parameters from URL

chrisclayton 05-12-16
Accepted Solution

Remove parameters from URL

We have a test runinng that adds a parameter to a URL for 50% of our users. This is working correctly howvever when the users click through to the payment page (2 clicks) they are presented with an error.


We have found the added paramters get passed through all the pages and this is what is breaking the bookings.

Can you please advise how we remove the added parameter in the Experiment for subsequent pages?


We need to remove the "&viewtype=roomview&&promoCode=" from the end



Example of URL and parameters to remove are:




Happy for this to be a new experiment if this is the only way to remove paramters from a URL. 


Thanks in advance.

MartijnSch 05-12-16

Re: Remove parameters from URL

Hey Chris,

What you could is check out this StackOverflow post on how to remove the parameters. What you need to do anyway is write some custom JavaScript for your test in order to remove them:

Re: Remove parameters from URL

Hey @chrisclayton, I have some experience with this and optimizely. First question, are you adding the parameters via optimizely? 


Re: Remove parameters from URL

Thanks for the reply,


Yes, we add the &viewtype=roomview paramter via Optimisely on the previous page.

Re: Remove parameters from URL

OK I see, 

Could you post the snippet of JS/Jquery that you use to do this? Essentially, you should be able to use the same snippet of code to remove it, but let's see what you have...


Re: Remove parameters from URL

The developer of the site managed to resolve this without the need for an experiment....sorry.

Re: Remove parameters from URL

[ Edited ]

No worries, I'm glad it worked out!

For future reference for anyone who stumbles into the issue of URL manipulation,

Here's a snippet of code that can add a query parameter via the experiment. I just want to point out, that doing this through optimizely does not guarantee that your web property will be able to utilize this parameter on the current loaded page.

Disclaimer: This is pureJS, so it will cause Optimizely to wait for DOMREADY to load unless you tell optimizely to force it to run!

var windowURL = window.location.href;
windowURL += "&[QueryParameterGoesHere]=[QueryParamValue]";
window.history.pushState(null, null, windowURL);

So, taking that a step further, if you want to remove the query param, there's multiple ways to do this, the easiest is to grab the window url again, and use javascript to search the string to remove the specific query parameter, and then append it back. In the instance above, we simply appended it to the end of the string, therefore if I know the length of the Query Parameter String that we initially added on, all I have to do is take a substring of a specific length as so:

var oldWindowURL = window.location.href;
var newWindowURL = oldWindowURL.substring(0, oldWindowURL.length - [LengthOfQueryParameter]);
window.history.pushState(null, null, newWindowURL);

Taking that a step further, you can go ahead and attach those actions to buttons/user actions, a timer, whatever you want. Once again, it is PureJS, it's not perfect, but for quick testing purposes - it works.

If anyone wants to elaborate on that solution, feel free!


JasonDahlin 05-17-16

Re: Remove parameters from URL

[ Edited ]

Hi @parththakkar,

A couple updates for your first script:

1- there current URL may or may not already have URL parameters.  Using & only works if they already exist.

2- don't forget about dealing with hash values too (those anchors that appear at the end of the URL after parameters).


Revised snippet for adding a name/value pair to the current URL (I dropped it into a function so that none of the variables make it all the way to the DOM except for the final result.

var newURL = (function(){
var Hash = window.location.hash;
var URLBase_and_QS = window.location.href.split('#')[0];
var newParams = (URLBase_and_QS.indexOf('?')==-1) ? '?' : '&';
newParams += "[QueryParameterGoesHere]=[QueryParamValue]";
return URLBase_and_QS + newParams + Hash;


Edit: updated the anonymous variable names a bit to make it clearer as to what each contains.  Notably, changed "windowURLBase" to "URLBase_and_QS" to show that it contains any pre-existing QueryString parameters)

--Jason Dahlin
Analytics and Testing Guru Smiley Happy

Experimentation Hero

Re: Remove parameters from URL

@JasonDahlin That's legit! Nice! Thank You!