Given the following URL:
https://example.com/test/results/?usp-custom-14=2015+2017&usp-custom-8=United-States
How can I save three vars:
var date1: "2015";
var date2: "2017";
var loc = "United States";
Note: we have two dates with a +
symbol in the url 2015+2017
and we need to split them. And has a dash in the url United-States
and we need it as United States
This is what I am trying:
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
var dates = getUrlVars()["usp-custom-14"];
var loc = getUrlVars()["usp-custom-8"];
var dateSplit = dates.split("+");
Also, I need to update the URL again for other reasons once the page is loaded, and I do:
history.replaceState('data to be passed', 'Title of the page', '<?php echo getAddress(); ?>/?usp-custom-14='+dateSplit+'&usp-custom-8='+loc);
But the url is duplicated
https://example.com/test/results/?usp-custom-14=2015+2017&usp-custom-8=United-States/?usp-custom-14=2015,2017&usp-custom-8=United-States