Function wrapper for addEvelentListener

Hello,


window.addEventListener( "DOMContentLoaded", function() {
          //...
        }, false);

I’d like to wrap this bit of code in a function:


var onReady = function()
    {

        window.addEventListener( "DOMContentLoaded", function() {
          //...
        }, false);

    }

I’d like then to be able to use the ready as follow:


onReady(
 //do stuff here
);

Obviously, this is not working. I’ve been searching for functions wrapper for addEventListener but didn’t come up with anythings satisfying. What would be the correct approach/syntax to wrap an eventListener in a function, and then use that function instead of the event listener?

Thanks in advance.

:slight_smile:

From what i can gather you are wanting to run the onReady function when the DOMContentLoaded event is triggered, see the below.

var onReady = function(event) {
    // Do something here...
};

window.addEventListener('DOMContentLoaded', onReady, false);