jQuery document ready events and TurboLinks

Thoughts from the team
By    | October 3, 2012 | Code Snippets,

Rails 4 will be shipping with TurboLinks enabled by default.  TurboLinks is a PJAX like library that asynchronously requests the content of the next page and inserts it into the current page’s body instead of requiring a full page reload.   It speeds up page load times nicely.  However, because the page is reloaded, the DOMContentLoaded event is triggered and your jQuery document.ready event wont be triggered.

The Solution

var do_on_load = function() { 
  // do some things 
}
$(document).ready(do_on_load)
$(window).bind('page:change', do_on_load)

You have to listen for the page:change event and bind your function to that too.  Simple.

More like this