Turbolinks load event not working on with page refresh

前端 未结 3 2024
离开以前
离开以前 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.

    0 讨论(0)
  • 2020-12-19 04:56

    You can use in this manner

    document.addEventListener('load turbolinks:load',function() {
    //Your code
    });
    

    equivalent coffee code

    document.addEventListener 'load turbolinks:load', ->
      #Your code
      return
    
    0 讨论(0)
  • 2020-12-19 04:58
    $(document).on('turbolinks:load', function() {
      // enter code here
    });
    
    0 讨论(0)
提交回复
热议问题