javascript loading only after refreshing page

♀尐吖头ヾ 提交于 2019-12-08 05:23:08

问题


I'm using Rails 4. I have a javascript file, app/assets/javascripts/exotic_fonts.js, which adds some javascript functionality to fonts. 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. However if I refresh the page, then the javascript file is loaded and the functionality is back. I was wondering how I can fix it.

Also, is it possible to load only some of the javascript files on a certain page?

Thanks.


回答1:


You can use following snippet to fix this issue. Now your js code will works with turbolinks.

ready = ->

// Your javascriptcode goes here

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


来源:https://stackoverflow.com/questions/19635355/javascript-loading-only-after-refreshing-page

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