No commit activity in last 3 years
No release in over 3 years
A short extension of Sprockets to wrap JS libs so they wait for DOMContentLoaded
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 2.0, <= 4
 Project Readme

Onload Transformer for Sprockets

What?

It's a small transformer for Sprockets that allows you to make a JS file wait for the document's DOMContentLoaded instead of running right when read.

Why?

Because vendors sometimes provide minified JS that's meant to be copy-pasted at the end of your page, but you don't want to do that because of turbolinks, personal conviction or other, but if you serve it through your asset pipeline you end up running it before your DOM is ready. Or you can use it to never write a $(document).ready again in your life I guess? I'm not the boss of you, use it for whatever you find it useful for.

How? (does it work)

It just wraps the whole file in a function and then binds it to the DOMContentLoaded event (with the proper check beforehand in case the DOM is already loaded).

How? (do i use it)

  • Add the gem to your gemfile.
  • Use the extension .onload.js instead of .js for your files.

What? (can be improved)

Probably a lot of stuff, but at least:

  • The functions used to wrap the body of the file are in the global namespace. It would be better if they were defined on a transformer-only object.
  • Make it so it can be used with .js.erb / .js.coffee (maybe ?)