使用 ELM 检测 IE 浏览器兼容模式

Detect IE browser compatibility mode on with ELM

我在 Elm 中有一个应用程序 运行。 我需要在 "IE compatible" 模式下加载应用程序之前向用户显示一些警告消息,因为打算不支持,仅特定于 IE 兼容模式。

用JavaScript我可以做到

navigator.userAgent.indexOf('compatible') > -1 ? true : false

但是有没有办法在 ELm 中自己做到这一点,就像 Main.init 中的第一件事一样?

我曾尝试使用 Elm 端口,但应用程序本身在到达端口之前就失败了,这就是为什么即使在任何 Elm 代码运行之前也必须检查浏览器兼容性的原因。

谢谢

一种方法是使用 flags。标志是当您首次从 JavaScript:

初始化应用程序时可以传递到 Elm 的值
  var app = Elm.Main.init({
    node: document.getElementById('myapp'),
    flags: {
      compatibilityMode: navigator.userAgent.indexOf('compatible') > -1
    }
  });

并在 Elm 端传递给 init 函数:

type alias Model = { compatibilityMode : Bool, ... }

type alias Flags = { compatibilityMode : Bool }

init : Flags -> ( Model, Cmd Msg )
init flags =
  ( { compatibilityMode = flags.compatibilityMode, ... }
  , Cmd.none
  )

...

main : Program Int Model Msg
main =
  Browser.element
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }