3

I am getting a strange problem by using jQuery's ready function.

I have a Ruby on Rails 4 application, and jQuery JavaScript Library v1.10.2, with some static pages that responds to the following urls:

http://localhost:3000/static_pages/home

http://localhost:3000/static_pages/about_us

I am using the following code for the javascript:

jQuery(document).ready( function($) {
  alert('document ready');
});

The problem is that it only fires when I refresh the page with the browser button, while it doesn't work if I follow one of the above links on the html page..

What's the problem?

mgambella
  • 152
  • 1
  • 5
  • How are you loading the jQuery library? Are you, by any chance, loading it dynamically rather than just specifying it in a ` – jfriend00 Jan 30 '14 at 05:55
  • Do you have any other javascript in your page? – Rico Jan 30 '14 at 05:57
  • 1
    Probably caused by turbolinks - check out some of the answers here: http://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links – house9 Jan 30 '14 at 06:03
  • 1
    @house9 yes, you are right: the problem was caused by turbolinks, and the link you provided is the exact solution to my problem. – mgambella Feb 01 '14 at 01:17

1 Answers1

0

Specify your document ready event handler correctly. May that could be the reason,

<script type="text/javascript">
   $( document ).ready(function() {
     alert('document ready');
   });
</script>

Also, hope your jquery file is being served through asset pipeline. Then do check that it is being served as given below.

{your APP_LOCALHOST_URL}/assets/jquery.js

If it is loading the file, then jquery is being served. otherwise, not.

H2h :)-

Rajesh Omanakuttan
  • 6,788
  • 7
  • 47
  • 85