Search code examples
urlsingle-page-applicationelm

How to create an url from an absolute path in elm


I want to create an url (not a link) for a text field for a share functionality. Like for example the one stackoverflow uses

des

I already hava function that produces the path part for the url, like

toUrl : Route -> String
toUrl route = ...

toUrl (Home (Just "hallo")) --> "/?b=hallo"

and using this string for a link as a href attribute works, but I'm wondering how I could create a complete url from this string.

PS: I'm using a single page application so I get an Url at the beginning.


Solution

  • What you say is "the path part for the url" isn't actually just the path part, but the path and query parts of the URL. Ideally you'd separate them so you can create a well-formed URL representation:

    { initialUrl
        | path = "/"
        , query = Just "b=hallo"
    }
    

    But since it's just a record, with no validation, it'll work if you just use it as the path as well. At least if you later just use Url.toString on it. Other operations might cause unexpected results.

    { initialUrl | path = "/?b=hallo" }