Ruby on Rails 4 javascript not executed

蹲街弑〆低调 提交于 2019-12-09 06:40:07

问题


I have a custom js file in app/assets/javascripts. This is the js file:

//app/assets/javascripts/contacts.js
//$(document).ready(function() { //I've already tried with this method

$(window).load(function() {
    alert("foo bar")
});

I require the file contacts.js file in the application.js file.

If I inspect the html page I see the js file loaded correctly, but the message is not shown. If I reload the page (by pressing f5), the message is correctly show.

When the page is loaded the javascript is loaded (I can see it in source html code in the browser) but not executed.

Can you help me?

SOLUTION: Rails 4: how to use $(document).ready() with turbo-links


回答1:


$(document).ready(function() { } # Not working with turbo-links

From

Turbolinks overrides the normal page loading process, the event that this relies on will not be fired. If you have code that looks like this, you must change your code to do this instead:

$(document).on('ready page:load', function () {
  // you code here
});

Another question



来源:https://stackoverflow.com/questions/22811654/ruby-on-rails-4-javascript-not-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!