5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Uncaught TypeError: Cannot read property 'nodeType' of null(in Elm)

Last updated at Posted at 2019-07-08

まとめ

とりあえず全部のscriptタグにdeferでもつけてみる

問題

image.png

image.png

画面がまっしろで開発者ツールのコンソールにUncaught TypeError: Cannot read property 'nodeType' of nullって出る

状況

<!DOCTYPE html>
<html lang="ja">
  <head>
    <script type="text/javascript" src="/elm.js"></script><!-- elm makeで生成したやつ -->
    <script type="text/javascript" src="/index.js"></script><!-- この中でElm.Main.initしてる -->
  </head>
  <body></body>
</html>

あんまりないと思うけどこんな状況
ElmのコードをJS側にくっつけてても一緒

解決法

1 deferったりする

<!DOCTYPE html>
<html lang="ja">
  <head>
    <script type="text/javascript" src="/elm.js" defer></script>
    <script type="text/javascript" src="/index.js" defer></script>
  </head>
  <body></body>
</html>

最低限Elm.main.initしてるほうにはdefer付ける必要があります

2 下の方に置く

<!DOCTYPE html>
<html lang="ja">
  <head>
    <script type="text/javascript" src="/elm.js"></script>
  </head>
  <body><script type="text/javascript" src="/index.js"></script></body>
</html>

deferなんて知らないってひとにおすすめ

原因

別にElm関係ない。DOMをマウントするときにちゃんと対象のDOMが存在するかどうかって話

jsのパースとかDOMのできるタイミングとかいろいろあるけど詳しくは他で調べてください。大事なのはDOMができたあとでinitするってことです

deferはjsの実行をDOMができたあとにするのでそれで通ります
詳細を気にしなければ何も難しいことはないですね

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?