1

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.

3
  • Are you using turbolinks? Commented Oct 28, 2013 at 13:47
  • Yes, I think Rails 4 uses turbolinks by default. Commented Oct 28, 2013 at 13:53
  • 3
    Turbolinks can cause problems with JavaScript. I would suggest you switch it off to see if the problem continues. If you decide to keep using it, take a look at this page: reed.github.io/turbolinks-compatibility Commented Oct 28, 2013 at 13:57

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Murtza, Thanks for your answer. I tried it but it didn't work. Although I included all the javascript in the ready function, the only explanation is that maybe I'm missisng something. I tried the jquery.turbolinks gem and everything worked. Is there a downside to using jquery.turbolinks? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.