Search code examples
elm

How to get query parameters in Elm?


In my Elm program, I'd like to initialize my model based on the query string.

For example, if the query string is ?w=3&h=5 I'd like to have:

initialModel =
  { width = 3
  , height = 5
  }

Is that possible to achieve this in Elm, or the only way to do this is to get the query parameters in Javascript and pass them via a port?


Solution

  • There is no built-in core library way to access the URL. You can use ports and the community library jessitron/elm-param-parsing.

    If you also want to set the URL, you can again use ports, or you can use the History API, for which there are bindings in TheSeamau5/elm-history.