use standard URLSearchParams insead of custom functions

This commit is contained in:
s2
2020-01-03 18:29:29 +01:00
parent b7fa481dcb
commit 45810382b9
67 changed files with 40893 additions and 47 deletions

View File

@@ -4,34 +4,8 @@
// utility functions
MyApp.Utils.getUrlParameter = function(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
MyApp.Utils.removeURLParameter = function(url, parameter) {
//prefer to use l.search if you have a location/link object
var urlparts = url.split('?');
if (urlparts.length >= 2) {
var prefix = encodeURIComponent(parameter) + '=';
var pars = urlparts[1].split(/[&;]/g);
//reverse iteration as may be destructive
for (var i = pars.length; i-- > 0;) {
//idiom for string.startsWith
if (pars[i].lastIndexOf(prefix, 0) !== -1) {
pars.splice(i, 1);
}
}
return urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : '');
}
return url;
};
// MyApp.Utils.someUtilityFunction = function() {}
// MyApp.Utils.someOtherUtilityFunction = function() {}
// app functions
MyApp.renderShell = function() {
@@ -94,8 +68,8 @@
cookieMinutes: 5256000
},
whitelist: ['en', 'de', 'it'],
fallbackLng: 'it',
load: 'languageOnly',
fallbackLng: 'it',
backend: {
loadPath: '/i18n/{{lng}}.json'
}
@@ -103,10 +77,12 @@
.then(function() {
// language initialized
// if there is a lang query string parameter, remove it and reload: we save the language in a cookie so the url stays nice
if (MyApp.Utils.getUrlParameter('lang')) {
var newUrl = MyApp.Utils.removeURLParameter(window.location.toString(), 'lang');
//removeURLParameter does not return the hash. add it back if there is one.
newUrl = newUrl + window.location.hash;
var urlQueryString = new URLSearchParams(window.location.search);
if (urlQueryString.has('lang')) {
urlQueryString.delete('lang');
var newUrl = [location.protocol, '//', location.host, location.pathname].join('') +
urlQueryString.toString() +
window.location.hash;
window.location = newUrl;
return;
}