DOMContentLoaded is triggered with Turbolinks

182 views
Skip to first unread message

Sean Liu

unread,
May 3, 2015, 8:34:47 PM5/3/15
to rubyonra...@googlegroups.com
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.

But I found DOMContentLoaded event is actually triggered when javascript
is placed at the bottom of body tag, so for example, DOMContentLoaded
will be triggered if I switch between pages using the following layout:

<!DOCTYPE html>
<html>
<head>
<title>Turbolinks</title>
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="container">
<%= yield %>
</div>
<%= javascript_include_tag 'application' %>
</body>
</html>

However, the DOMContentLoaded won't be triggered if javascript is placed
in head tag. I read the source code of Turobolinks gem but I still don't
understand why. Thanks!

--
Posted via http://www.ruby-forum.com/.

Hunter Stevens

unread,
May 15, 2015, 10:53:38 AM5/15/15
to rubyonra...@googlegroups.com
Does Turbolinks still work, and make page loading faster, when you do this? I am about to try this, because I am having issues using `sorttable.js` in our app with TL turned on
Reply all
Reply to author
Forward
0 new messages