0
0

More than 1 year has passed since last update.

Turbolinks使用環境では$(document).on('turbolinks:load')にする

Posted at

Railsで

$(windows).on('load', function(){...})

ではなく

$(document).on('turbolinks:load', function(){...})

とすることで期待通り発火してくれるようになることがありますが、その理由の備忘録です。

前提

Ruby: 2.7.2
Rails: 6.1.6
gem: turbolinks 5.2.1

理由

turbolinksではページ遷移を高速化してくれますが、仕組みとしてはブラウザでページ遷移が起きた時に

  • head内で差異があればマージ
  • bodyタグを差し替え

することで、同ページ内であたかも画面遷移が起きているように見せています。
実際にはページ遷移が起きていないため、ページ遷移が発火条件の$(windows).on('load', function(){...})は発火しません。

なので、turbolinksを使用する場合のページ遷移は、turbolinksで用意されたロードイベントturbolinks:loadを使用する必要があります。

参考

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