Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo-mod-jslibs-dist.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'alpinejs/packages/history/src/url.js')
-rw-r--r--alpinejs/packages/history/src/url.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/alpinejs/packages/history/src/url.js b/alpinejs/packages/history/src/url.js
new file mode 100644
index 0000000..7e2b4b6
--- /dev/null
+++ b/alpinejs/packages/history/src/url.js
@@ -0,0 +1,36 @@
+
+export function hasQueryParam(param) {
+ let queryParams = new URLSearchParams(window.location.search);
+
+ return queryParams.has(param)
+}
+
+export function getQueryParam(param) {
+ let queryParams = new URLSearchParams(window.location.search);
+
+ return queryParams.get(param)
+}
+
+export function setQueryParam(param, value) {
+ let queryParams = new URLSearchParams(window.location.search);
+
+ queryParams.set(param, value)
+
+ let url = urlFromQueryParams(queryParams)
+
+ history.replaceState(history.state, '', url)
+}
+
+function urlFromParams(params = {}) {
+ let queryParams = new URLSearchParams(window.location.search);
+
+ Object.entries(params).forEach(([key, value]) => {
+ queryParams.set(key, value)
+ })
+
+ let queryString = Array.from(queryParams.entries()).length > 0
+ ? '?'+params.toString()
+ : ''
+
+ return window.location.origin + window.location.pathname + '?'+queryString + window.location.hash
+}