How to use several onload in the same html file


Let's see if someone can help me.

I want to use in a file html several different scripts in vanilla javascript and I find the problem of using the functions when the document is loaded with the instruction:

document.onload = function (){

Having several onload only the last one is executed, so it's not what I'd like.

Do you recommend me to do something?

asked by bermartinv 01.03.2017 в 14:38

2 answers


You can do the following. Create a single event onload that calls a function that loads the other functions.

For example:

function cargarFunciones(){

With this you can load different functions with the same onload .

answered by 02.03.2017 в 15:15

As commented in other answers you should have only a onload() and you can make it trigger other functions that perform certain tasks. I would recommend using the Revealed Module pattern.

You can get more information at this link . Basically what this pattern does is give it a name space "namespacing" and be able to modularize everything to execute it just when you need it.

var miApp = ( function(){
  var metodoPrivado = function(){
    console.log( 'Privado: ', this);
    return 'Soy privado';

  return {
    metodoPublico : function(){
      console.log( 'Publico: ', this);

      // Invocando el metodo privado:

      return 'Soy publico';


I hope you find it useful.


answered by 01.03.2017 в 18:06