From ce6f5efc38e674a4d38aeca6f4d55d8633f93a80 Mon Sep 17 00:00:00 2001 From: MattMo Date: Wed, 4 May 2022 09:03:47 -0700 Subject: [PATCH] Added removeParameter function that can remove a query parameter from the route. --- ignite-html-router.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ignite-html-router.js b/ignite-html-router.js index 344e744..daf9492 100644 --- a/ignite-html-router.js +++ b/ignite-html-router.js @@ -472,6 +472,24 @@ class Router { return (params.has(name) ? params.get(name) : null); } } + + /** + * Removes a query parameter from the route search if it exists. + * @param {String} name + */ + static removeParameter(name) { + if (Router.hashMode) { + var params = new URLSearchParams(window.location.hash.includes("?") ? "?" + window.location.hash.split("?")[1] : ""); + if (params.has(name)) { + params.delete(name); + window.location.hash = window.location.hash.split("?")[0] + "?" + params.toString(); + } + } else { + var url = new URL(window.location); + url.searchParams.delete(name); + window.history.pushState(null, '', url.toString()); + } + } } Router.states = [];