Access URL query string in svelte
Asked Answered
B

6

42

How should I access the the query string parameters from svelte? I'd like my script to behave differently when "?beta" has been appended to the URL.

My intuitive approach would be to use the standard URLSearchParams in a svelte #if block.

Bronchial answered 15/3, 2021 at 12:3 Comment(1)
$page.url.searchParams.get('xxx') to get a url query param.Mathew
P
65

Yep, you should be able to use URLSearchParams. In general, anything you can do in plain JS you can do in a Svelte script tag.

<script>
    const urlParams = new URLSearchParams(window.location.search);
    const isBeta = urlParams.has('beta');
</script>

{#if isBeta}
    <p>This is beta!</p>
{:else}
    <p>This is not beta.</p>
{/if}

Edit: the above method will not work in SvelteKit, which enables server-side rendering by default. In SvelteKit, you should use the page store, which exposes a native URL object.

<script>
    import { page } from '$app/stores';
    
    const isBeta = $page.url.searchParams.has('beta');
</script>

This store was changed as recently as SvelteKit v1.0.0-next.208, so other answers referencing it may be out of date. In particular, $page.query is no longer available since it was replaced by $page.url.

Pleopod answered 15/3, 2021 at 15:45 Comment(1)
Cannot read properties of undefined (reading 'searchParams') when i use itHeuser
R
50

Here's how you do it with in SvelteKit:

<script>
    import { page } from '$app/stores'
    const email = $page.url.searchParams.get('email')
</script>
Reproductive answered 18/8, 2021 at 22:32 Comment(7)
$page.query has been replaced by $page.url.searchParamsCuticula
how do you use this?Heuser
import {page} from '$app/stores'; const id = $page.url.searchParams.get('id');Toback
Cannot read properties of undefined (reading 'searchParams')Heuser
@Cuticula can you provide a link to this in the docs? I can't seem to find any reference to searchParams or the URL type that is attached to $page.Reproductive
@Reproductive I can't find it in the docs either... but Geoff Rich's answer cites the this PR, which replaces (among other things) [query] with the url object.Cuticula
The official SvelteKit docs for URL APIs is available here and agrees with this answer: kit.svelte.dev/docs/web-standards#url-apis const foo = url.searchParams.get('foo');Womanizer
H
14

For anyone specifically using SvelteKit, a Svelte framework, the page store lets you import a page object allowing you to access the parameters as page.query:

<script>
  import { page } from '$app/stores';
  let isBeta = page.query.beta;
</script>


{#if isBeta}
  <p>This is beta!</p>
{:else}
  <p>This is not beta.</p>
{/if}
Howler answered 11/8, 2021 at 21:51 Comment(2)
You need the page.query.get("beta") for it to workVirescent
this is deprecatedHeuser
C
8

As of June 2022:

<script>
  import { page } from '$app/stores';
  const beta = $page.url.searchParams.get('beta');
</script>
Cuticula answered 4/6, 2022 at 6:55 Comment(0)
E
3

In 2023 using SvelteKit,

<script>
import { page } from '$app/stores';
const beta = $page.url.searchParams.get('beta');
</script>

{#if typeof beta === 'string'}
Beta is on!
{:else}
No beta :(
{/if}

Your life would be bit easier when you use ?beta=1 or any value that is considered true. In snippet above was needed to check if it is an (empty) string.

Elative answered 18/3, 2023 at 3:4 Comment(0)
S
2

A little old, window is somewhat more involved with server side rendering as indicated here: I think this might be better:

<script>   
    import { page } from '$app/stores'
    let urlPrams = $page.query.get('beta')
    const isBeta = false
    if (urlPrams && urlPrams.length > 0) {
         const isBeta = true
    }
    
</script>

{#if isBeta}
  <p>This is beta!</p>
{:else}
  <p>This is not beta.</p>
{/if}
Spade answered 8/11, 2021 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.