3

How do I detect the browser from Elm?

Specifically I want to be able to tell if the web app is running on a tablet (Safari on iPad, etc.) or not.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187

2 Answers2

8

You can use Html.programWithFlags to pass information from Javascript to Elm on initialization.

Assuming you can infer browser from user agent, you could do something like this:

type alias Flags =
    { userAgent : String }

Your init would look like this:

init : Flags -> ( Model, Cmd Msg )
init flags =
  ...

main =
    programWithFlags { init = init, ... }

And from Javascript, you would pass the flags in like this:

var app = Elm.Main.fullscreen({
    userAgent: navigator.userAgent
});

Side note: User agent may not be enough to fully detect browser. You can see this StackOverflow answer which provides more reliable detection. Either way, the end result is that you would send some kind of flag along to the Elm app on init.

More info on Flags can be found here.

Community
  • 1
  • 1
Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97
1

You can use elm-vendor package.

http://package.elm-lang.org/packages/coreytrampe/elm-vendor/latest

pdamoc
  • 2,798
  • 15
  • 19
  • 1
    The undocumented Native (Kernel) code folks used for this kind of thing is no longer available as of 0.19 as it conflicts with DCE. – Mario Dec 23 '19 at 08:05