You can put in the values
only the value of the variable that the code will normally capture. I've also added an empty% wrapper which will be the default if there is no variable in the URL:
<select id="layout" name="layout">
<option value="">Layout</option>
<option value="1">Layout 1</option>
<option value="2">Layout 2</option>
<option value="3">Layout 3</option>
<option value="4">Layout 4</option>
</select>
<select id="sidebar" name="sidebar">
<option value="">sidebar</option>
<option value="1">sidebar 1</option>
<option value="2">sidebar 2</option>
<option value="3">sidebar 3</option>
<option value="4">sidebar 4</option>
</select>
And the code that will change the variables and links and make the refresh on the page when some select changes:
document.addEventListener("DOMContentLoaded", function(){
var url_ = location.href,
param = url_.substring(url_.lastIndexOf("/"), url_.length),
params = ['layout','sidebar']; // insira aqui os nomes das variáveis
for(var y=0; y<params.length; y++){
if(param.indexOf(params[y]) != -1){
var var_ = url_.substring(url_.indexOf(params[y])+params[y].length+1,url_.length).match(/^(\d|\w){1,}/)[0],
a_ = document.body.querySelectorAll("a");
document.body.querySelector("#"+params[y]).value = var_;
for(var x=0; x<a_.length; x++){
a_[x].href += (a_[x].href.indexOf("?") == -1 ? "?" : "&")+params[y]+"="+var_;
}
}
}
var sels = document.querySelectorAll("select");
for(var x=0; x<sels.length; x++){
sels[x].addEventListener("change", function(){
var sId = this.id,
sVa = this.value;
if(sVa && url_.indexOf(sId) == -1){
location.href = url_+(url_.indexOf("?") == -1 ? "?" : "&")+sId+"="+sVa;
}else if(sVa && url_.indexOf(sId+"="+sVa) == -1){
var var_ = url_.substring(url_.indexOf(sId)+sId.length+1,url_.length).match(/^(\d|\w){1,}/)[0];
location.href = url_.replace(sId+"="+var_, sId+"="+sVa);
}
});
}
});
21.02.2018 / 18:06