Ruby on Rails 4 javascript未执行 [英] Ruby on Rails 4 javascript not executed

查看:131
本文介绍了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
});

另一个问题

这篇关于Ruby on Rails 4 javascript未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆