News & Views.

The latest news, web designs and digital projects happening at Storm.

Sharing is caring:

jQuery document ready events and TurboLinks

Adam Pope Profile Image

Adam Pope
October 3, 2012

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.