I have a button in body that once clicked, it should add an extra query parameter in the URL.
let addParam;
const button = document.createElement("BUTTON");
button.textContent = "Add";
document.body.prepend(button);
button.addEventListener("click", () => {
const url = new URL(window.location.href);
url.searchParams.set("param", "value");
window.history.pushState({ path: url.href }, "", url.href);
addParam = addParam ? false : true;
});
It works fine, the problem is that the current URL contains already a query parameter in the form of ?path=path/to/file, so when we set another parameter with set(key, value), it changes the ?path=path/to/file to ?path=path%2Fto%2Ffile. Is there a way I can prevent that from happening and keeping the original URL?
path%2Fto%2Ffileis perfectly valid. It is properly encoded! If whatever is using that value has a problem with it, then code should be fixed to decode it properly.addParam = addParam ? false : true;isaddParam = !addParam;:-)