11

How can I redirect to external url? I've tried with Navigation module, but it seems to work only for local routes. Is there a way to do it natively, without js ports?

I.e. window.location.href = http://google.com;

Edit: I somehow missed Navigation.load function. As suggested below, it will help with redirects.

Ilya
  • 1,120
  • 1
  • 12
  • 33

1 Answers1

14

Since a tag can be always used with specified href, I'd rather try to find a solution which would avoid using redirection from update function.

a [ href "http://google.com" ] [ text "Google link" ]

But in case it's necessary to implement the logic similar to window.location.href = "http://google.com";, elm-lang/navigation provides load function (and a couple of other useful ones for forcing page loads) which does, what you're expecting.

It takes a url and returns a command load : String -> Cmd msg, so it's going to look like this:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        RedirectToGoogle ->
            ( model, load "http://google.com" )
gdejohn
  • 7,451
  • 1
  • 33
  • 49
Igor Drozdov
  • 14,690
  • 5
  • 37
  • 53
  • Thank you for the answer. I need to redirect once the back-end completes the task, so tag is not suitable. But Navigation.load did the job! I somehow missed it :( Have a nice day! – Ilya Sep 20 '17 at 07:36