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 onlyturbolinks?? Because it should work with bothturbolinks and jquery-turbolinks after including jquery.turbolinks in your application.js file.
    – Justin
    CommentedApr 3, 2014 at 13:05
  • @Justin: If we don't use jQuery-turbolinks?.
    – Shane
    CommentedApr 3, 2014 at 13:09
  • check my answer, it should workCommentedApr 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() { ... }); 
2
  • What is this page:load ? Is it a jQuery thing or turbolinks?
    – Shane
    CommentedApr 3, 2014 at 13:34
  • It's a browser event provided by turbolinks, triggered when you navigate to a different page.CommentedApr 3, 2014 at 13:42
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
  • What is this page:load ? Is it a jQuery thing or turbolinks?
    – Shane
    CommentedApr 3, 2014 at 13:32
0

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

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

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.