README.md
This lets you load application JavaScript asynchronously from `<head>`. Compatible with Turbolink v2, v5.0, v5.1, jquery-turbolinks, and no Turbolinks. Compatible with all browsers and IE9+ (only IE10+ will actually benefit from this though).
How to use this:
1. Add the [HTML snippet](#file-_end_of_body-html-erb) just before the closing `</body>` tag.
2. Add the [on_page_load.js](#file-on_page_load-js) script to the beginning of your `application.js`, and use `window.App.onPageLoad(...)` instead of `document.addEventListener('DOMContentLoaded', ...)` and `jQuery(($) -> ...)` everywhere.
3. Include your JavaScript like so:
```erb
<head>
This file has been truncated. show original
_end_of_body.html.erb
<%# If the app JS is loaded via an [async] script, the JS may
run before or after DOMContentLoaded. Expose a flag, so it can
initialize correctly. %>
<script data-turbolinks-eval="false">
document.addEventListener('DOMContentLoaded', function() {
(window.App = window.App || {}).DOMContentLoadedFired = true;
});
</script>
on_page_load.js
(function() {
this.App = this.App || {};
var App = this.App;
var isTurbolinks = 'Turbolinks' in window && window.Turbolinks.supported;
var isTurbolinks5 = isTurbolinks && 'clearCache' in window.Turbolinks;
var onPageLoadFiredOnce = false;
var pageLoadCallbacks = [];
var triggerOnPageLoad = function() {
This file has been truncated. show original