Turbolinks load event not working on with page refresh

前端 未结 3 2037
离开以前
离开以前 2020-12-19 04:11

Javascript code like this

document.addEventListener(\"turbolinks:load\", function() {
  $(\"p#hide_if_js\").hide();
});

is working fine wit

3条回答
  •  忘掉有多难
    2020-12-19 04:54

    With help from uzaif's comment, the following worked

      ready = ->
        if $('body').attr('data-loaded') == 'T'
          return
        # code goes here
        $('body').attr('data-loaded','T')
      $(document).ready(ready)
      $(document).on('turbolinks:load', ready)
    

    The $('body').attr('data-loaded') lines are to prevent the code loading twice.

    This approach is using event delegation, as recommended by the turbolinks instructions. It is not clear why it is still necessary to use the $(document).ready(ready) as the turbolink:load is meant to cover this.

提交回复
热议问题