Search code examples
javascripthtmlcsswebconsole

want to use javascript functions while they are within window.onload


The addEventListener doesn't add events, I found that I should use

window.onload = function() {
  // whole code here
}

but, when I try to use the functions that are within the window.onload in the browser console, I can not, only the outer functions of it could be, I want to use them even if they were in it.


Solution

  • You can explicitly assign the functions to the window inside the handler.

    window.addEventListener('DOMContentLoaded', () => {
      window.someFn = () => {
        // ...
      };
      someFn();
      // you can also use someFn in the browser console
    });