简体   繁体   中英

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. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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