Ruby on Rails 4 javascript未执行

问题描述:

我在 app / assets / javascripts 中有一个自定义 js 文件。
这是js文件:

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")
});

我需要文件联系人。 application.js 文件中的js 文件。

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

如果我检查html页面我看到js文件正确加载,但是没有显示消息。
如果我重新加载页面(通过按f5),消息就会正确显示。

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.

当页面加载时,javascript被加载(我可以在浏览器的源代码中看到它)但是没有执行。

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

你能帮助我吗?

解决方案:
Rails 4:如何使用$(document).ready()和turbo-links

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

来自

From


Turbolinks会覆盖正常的页面加载过程,不依赖于
依赖的事件。如果您的代码看起来像
,则必须更改代码才能执行此操作:

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
});

另一个问题