Skip to content

Commit

Permalink
Safe-insert GET parameter to URL when changing language
Browse files Browse the repository at this point in the history
  • Loading branch information
fracz committed Sep 13, 2017
1 parent b55453e commit c604593
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Frontend/src/login/language-selector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,27 @@
},
methods: {
updateLocale() {
window.location.assign(window.location.href + '?lang=' + this.locale);
this.safeInsertGetParamToUrl('lang', this.locale);
},
// idea based on https://stackoverflow.com/a/487049/878514
safeInsertGetParamToUrl(key, value) {
key = encodeURI(key);
value = encodeURI(value);
const kvp = document.location.search.substr(1).split('&');
let i = kvp.length;
let x;
while (i--) {
x = kvp[i].split('=');
if (x[0] == key) {
x[1] = value;
kvp[i] = x.join('=');
break;
}
}
if (i < 0) {
kvp[kvp.length] = [key, value].join('=');
}
document.location.search = kvp.join('&');
}
}
};
Expand Down

0 comments on commit c604593

Please sign in to comment.