user2725109
user2725109

Reputation: 2386

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.

Upvotes: 1

Views: 957

Answers (1)

Murtza
Murtza

Reputation: 1416

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)

Upvotes: 1

Related Questions