Ruby on rails 4.0 + javascript not loading

前端 未结 3 508
故里飘歌
故里飘歌 2021-01-07 13:17

I have a javascript file called \"front.js\" i load from \"application.js\" like usual with this code:

//= require front

Inside \"front.js\

相关标签:
3条回答
  • 2021-01-07 13:35

    There are three ways by which you can do it.

    a. You can wrap your jquery code inside a function and then call that function on page load. If your function is ready then you can do:

    $(document).ready(ready);
    $(document).on('page:load', ready);
    

    b. You can use jquery's live event or "on" event:

    $(document).on("click","#test",function(){
     // your code
    });
    

    c. You can use the jquery-turbolinks gem which will bind the Rails Turbolink events to the document.ready events so you can write your jQuery in the usual way. For more information refer to here

    0 讨论(0)
  • 2021-01-07 13:36

    It's due to turbolinks, do this instead

    var ready;
    ready = function() {
      $("#test").click(function(event){
        ...
      });
    };
    $(document).ready(ready);
    $(document).on('page:load', ready);
    
    0 讨论(0)
  • 2021-01-07 13:45

    I had the same problem when using foundation in my project, you can disable turoblinks by not requiring turbolinks in your application.js file and it should solve the problem.

    0 讨论(0)
提交回复
热议问题