I have this script to replace a word:
var url = window.location.toString();
window.location = url.replace(/(\?sort=name|size)/, 'date');
and this one meant to append a text to the end of a string:
var orig = location.href;
location.replace(orig+'?sort=date');
How to combine them into one script so the text only gets appended when the ?sort parameter isn’t already there?
The ?sort parameter is supposed to be always at the end of a string, if that matters.
Side note: I think you meant
'?sort=date'instead of just'date'in your replace?So
window.location = re.test(url) ? url.replace(re, '?sort=date') : url + "?sort=date";