Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute JS every time on the event of turbolinks load in Rails 5 app

I would like to execute a few lines of js every time turbolinks is loaded(i.e, the page is loaded), not the js from cache, but would like to force run it before the event of turbolinks load, every time. Is that possible?

I'm trying to get rid of FOUC here, by hiding the html body and doing this in app.js.coffee

$(document).on 'turbolinks:load', ->
    document.body.style.visibility = 'visible'

But the FOUC starts kicking in after the 3rd or 4th time of reloading the page.

To see this live in action, here is a link to the website.

like image 461
Pre-alpha Avatar asked Oct 18 '22 05:10

Pre-alpha


1 Answers

Turbolinks only loads when using links on that page, not when refreshing the page or navigating via url. If you want to run js when the page is loaded then use $(document).ready(function() { executable js here });

like image 121
Joe Woodward Avatar answered Oct 22 '22 10:10

Joe Woodward