简体   繁体   English

仅在刷新页面后加载javascript

[英]javascript loading only after refreshing page

I'm using Rails 4. I have a javascript file, app/assets/javascripts/exotic_fonts.js, which adds some javascript functionality to fonts. 我正在使用Rails4。我有一个javascript文件,app / assets / javascripts / exotic_fonts.js,它为字体添加了一些javascript功能。 When I load the homepage this javascript file is automatically loaded on the homepage, however it's not loaded automatically on other pages such as /user/show and the functionality is missing. 当我加载主页时,此javascript文件会自动加载到主页上,但是不会自动加载到其他页面(例如/ user / show)上,因此功能会丢失。 However if I refresh the page, then the javascript file is loaded and the functionality is back. 但是,如果刷新页面,则将加载javascript文件,并且功能将恢复。 I was wondering how I can fix it. 我想知道如何解决它。

Also, is it possible to load only some of the javascript files on a certain page? 另外,是否可以在特定页面上仅加载某些javascript文件?

Thanks. 谢谢。

You can use following snippet to fix this issue. 您可以使用以下代码段解决此问题。 Now your js code will works with turbolinks. 现在,您的js代码将可用于turbolinks。

ready = ->

// Your javascriptcode goes here

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM