2

I've seen several posts on here, as well as blog posts, about this issue. None of them have helped me so far, eg this one: Same rails and turbolink version

Here's an example of js which doesn't load without refreshing the page:

document.addEventListener('turbolinks:load', function() {
    document.getElementById(id[0]).oninput=function () {
        validate(id[0]);
    }
});

The Javascript runs fine without Turbolinks, and removing Turbolinks is not a solution for me.

Note: It does work if I require a specific JS file from application.html.erb, but then it runs every file on all pages on the web site, and the JS has to be page specific.

Community
  • 1
  • 1
Aleins
  • 43
  • 8
  • Can you output anything to console via console.log? Remove everything from your turbolinks:load callback function and add console.log('test'); to see if that works. – l.varga Apr 26 '17 at 12:19
  • @l.varga document.addEventListener('turbolinks:load', function() { document.getElementById(id[0]).oninput=console.log('test'); }); Like This? Doesn't work, no – Aleins Apr 26 '17 at 12:22
  • not like this - just the console log to debug and to pinpoint where the actual source of your problems is, whether it's the turbolinks:load that doesn't get picked up by the listener (which, in my opinion, shouldn't be the problem), or the rest of your function (higher chance): document.addEventListener('turbolinks:load', function() { console.log('test'); }); – l.varga Apr 26 '17 at 13:01
  • Did what you suggested now, only works after refreshing the page still. – Aleins Apr 26 '17 at 13:46
  • Which file do you have to require from application.html.erb in order to make it work? Is it a turbolinks file? Because right now it seems turbolinks:load isn't being triggered and the only explanation that I can think of is that turbolinks isn't being executed. – l.varga Apr 26 '17 at 13:48

1 Answers1

0

You should use this one for turbolinks.

$(document).on('page:load', ready);

But this is quite bad, because on some pages you want use turbolinks, on other you don't want. Soon you will have script initialized just by document ready(because you cannot wait page to load), and other via page:load. So better just prevent this 'unexpected behaviour':

= link_to "Page", some_path(@page), data: { turbolinks: 'false' }
nautgrad
  • 414
  • 2
  • 8