LoginSignup
1
0

More than 3 years have passed since last update.

jQueryのready、load関連の実行順序

Last updated at Posted at 2016-11-30

jQueryを使っていて、ファイル内でのready・loadまわりの実行順序があやふやになるので、まとめておきたいと思います。以下coffeescriptです。

# どの行に書いても以下の種類の順番で動作します。

# ====================================
# 1
# ベタ書きが一番最初で、上から順に実行される
console.log '1'

# ====================================
# 2
# 以下の2つは同じで、上から順に実行される
# DOMが用意された時に実行される

$(document).ready ->
  console.log '2'

$ ->
  console.log '3'

# ====================================
# 3
# ※jquery 1.8以降では非推奨。
$(document).on 'ready',->
  console.log '4'

# ====================================
# 4
# loadは、画像なども含めて読み込み終了後
$(window).on 'load',->
  console.log '5'

知らなかったのは、$(document).on 'ready'が非推奨になっていたことでしょうか。

1
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
1
0