0

I know there is jQuery-turbolinks. But is there anyway to use jQuery and JS with turbolink.

https://github.com/rails/turbolinks

With Turbolinks pages will change without a full reload, so you can't rely on DOMContentLoaded or jQuery.ready() to trigger your code. Instead Turbolinks fires events on document to provide hooks into the lifecycle of the page.

I had this piece of code, but this did not load on navigating to other url.

$(function() {
  return $('.status').hover(function(event) {
    return $(this).toggleClass("hover");
  });
});
3
  • Are you asking if there is a way to use that function with only turbolinks?? Because it should work with both turbolinks and jquery-turbolinks after including jquery.turbolinks in your application.js file. Commented Apr 3, 2014 at 13:05
  • @Justin: If we don't use jQuery-turbolinks?. Commented Apr 3, 2014 at 13:09
  • check my answer, it should work Commented Apr 3, 2014 at 13:17

3 Answers 3

2

You can do the following to run your javascript on both page:load and ready.

$(document).on('page:load ready', function() {
    ...
});
Sign up to request clarification or add additional context in comments.

2 Comments

What is this page:load ? Is it a jQuery thing or turbolinks?
It's a browser event provided by turbolinks, triggered when you navigate to a different page.
1

Try it out ;)

var ready;
ready = function() {
  $(function() {
    return $('.status').hover(function(event) {
      return $(this).toggleClass("hover");
    });
  });
};
$(document).ready(ready);
$(document).on('page:load', ready);

1 Comment

What is this page:load ? Is it a jQuery thing or turbolinks?
0

This doc showing how to: https://github.com/turbolinks/turbolinks

document.addEventListener("turbolinks:load", function() {
      // ... Your code here!
    })

Comments

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.