1

I cannot get jquery to work for the life of me. I have two pages I'm working on, both require javascript.

The first page is my home page. It has simple coffeescript to display an alert on document load

$(document).ready ->
 alert('ready')

The second page has it's own coffeescript im not going to paste here.

When I load the home page this document.ready does not fire. However, if I load the second page, it's code AND this alert are fired.

What this leads me to believe is that jquery isn't being loaded. So I went ahead and tested a few things

  1. Jquery is the first javascript file included, the home page's javascript file is the second to last one included (last one is application.js).
  2. The second page's script file is included before the home page's script file
  3. Removing turbolinks does not fix it.

So I decided to see if I could get anything to work.

alert 'test'
$(document).ready ->
 alert('ready')

will fire the 'test' alert on the homepage.

Further, if I include script tags at the bottom of the home page and I put in some jquery to prevent form submission it works fine. Below is the code:

<script type="application/javascript">
  $("#formtest").submit(function(e){
  e.preventDefault();
  alert('No submit');
 });
</script>

However,

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

Still doesn't work. Which tells me that the DOM loaded event is not firing for the home page.

EDIT:

It appears the $(document).ready -> ... in the second page's javascript is screwing up this one. Why is that?

user3389436
  • 325
  • 1
  • 4
  • 8
  • possible duplicate of [JQuery doesn\`t fire/work with my Rails 3.2 project](http://stackoverflow.com/questions/9344569/jquery-doesnt-fire-work-with-my-rails-3-2-project) – Cjmarkham Mar 29 '14 at 21:23
  • @Carl The selected answer and that topic does not fix my problem. – user3389436 Mar 29 '14 at 21:24
  • the problem might be in turbolinks – kirqe Mar 29 '14 at 21:24
  • I removed turbolinks and the problem persists. Further, the second page has turbolinks enabled and document ready fires fine. – user3389436 Mar 29 '14 at 21:25
  • have you tried the solution found here: http://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links. This worked for me when I was having what sounds like the same problem – jackel414 Mar 29 '14 at 21:25
  • Mb that page has a different layout. And it has this line "data-turbolinks-track" => true in it – kirqe Mar 29 '14 at 21:56

1 Answers1

1

As I mentioned in my comment, a solution that worked for me was to have the jquery event fire this way as well:

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

An additional solution is also to install the following gem:

gem 'jquery-turbolinks'

Using that gem will allow you to use the DOM load event to trigger all necessary jquery functions.

jackel414
  • 1,636
  • 2
  • 13
  • 29
  • Unfortunately the jquery-turbolinks gem does not fix the problem. I can get an alert to show using $(document).on('page:load', alert 'lol') but I can't use any functions. – user3389436 Mar 29 '14 at 21:46
  • @user3389436: That `alert 'lol'` will execute when you call `.on`, not when the `page:load` event fires. – mu is too short Mar 29 '14 at 23:13