Skip to content

Instantly share code, notes, and snippets.

@evgv
Created September 24, 2016 10:01
Show Gist options
  • Save evgv/745ed8076d7dac12992382ad0922efac to your computer and use it in GitHub Desktop.
Save evgv/745ed8076d7dac12992382ad0922efac to your computer and use it in GitHub Desktop.
JS. Clear custom vaiables from url

Clear custom vaiables from url

The function takes a URL and then walked on given array elements as variable what will be cleared from the URL, the function returns prepared URL.

######How to use it, add this script on your site and call clearv.clear(YOU_URL):

  clearv.clear('http://example.com?utm_source=transactional&utm_medium=email&utm_campaign=digest-comments')

will return

  http://example.com
var clearv = {
/* array with vars fro clear from url */
variables: ['admitad_uid', 'acc','bannerid', 'utm_source', 'utm_campaign', 'utm_medium', 'utm_content', 'utm_term', 'yclid', 'gclid'],
/**
* Method clear from given url variabes and return prepared url
*
* @param {string} url
* @return {string} url
*/
clear: function(url) {
var self = this;
jQuery.each(self.variables, function(key, value) {
/* create regular expression */
var regex = new RegExp('([?&])' + value + '=[^&]+(&|$)', 'g');
/* replace given params on notjin */
url = url.replace(
regex,
function(match, contents, offset, s){
return offset === '&' ? contents : '';
}
);
});
return url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment