From eb04ecb5b4a87bd43fbffb9efb408d91f99796fe Mon Sep 17 00:00:00 2001 From: Molkobain Date: Sun, 18 Jul 2021 22:37:01 +0200 Subject: [PATCH] Fix new URL parameter being append after the '#' in CombodoGlobalToolbox.AddParameterToUrl --- js/utils.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/js/utils.js b/js/utils.js index 9e0fcbf97..966adf24b 100644 --- a/js/utils.js +++ b/js/utils.js @@ -713,19 +713,20 @@ const CombodoGlobalToolbox = { * @param sUrl {string} The URL to append the new param to * @param sParamName {string} Name of the parameter * @param sParamValue {string} Value of the param, needs to be already URL encoded - * @return {string} The sUrl parameters with the sParamName / sParamValue append at the right place + * @return {string} The sUrl parameter with the sParamName / sParamValue append at the end of the query string (but before the hash if any) */ AddParameterToUrl: function(sUrl, sParamName, sParamValue) { - const bHasHash = sUrl.split('#')[1] !== undefined; - const bHasSomeParameters = sUrl.split('?')[1] !== undefined; const sNewParamForUrl = sParamName + '=' + sParamValue; - if (bHasHash || bHasSomeParameters) { - sUrl += '&' + sNewParamForUrl; - } else { - sUrl += '?' + sNewParamForUrl; - } + // Split URL around the '#'. Note that if there are multiple '#' in the URL (which is not permitted!) this method won't work. + const aHashParts = sUrl.split('#'); + // Part of the URL starting from the protocol to the character before the '#' if one, to the end of the URL otherwise + const sPreHashPart = aHashParts[0]; + // Part of the URL starting just after the '#' if one, null otherwise + const sPostHashPart = aHashParts[1] ?? null; + + sUrl = sPreHashPart + (sUrl.split('?')[1] ? '&' : '?') + sNewParamForUrl + (sPostHashPart !== null ? '#' + sPostHashPart : ''); return sUrl; },