Skip to content Skip to sidebar Skip to footer

How Do I Run This Function On Page Load?

I have to wrap my functions in a namespace. How do I run my function on page load in this format? window.myFunction = { submitButton: function () { document.getElementById('button

Solution 1:

myFunction in your example is an object not a function and submitButton is a property of that object, to access it you'll do this:

window.onload = window.myFunction.submitButton;

update

Something like this will run multiple functions.

window.onload = function(){
    for(var prop inwindow.myFunction) { 
      if(window.myFunction.hasOwnProperty(prop) && typeofwindow.myFunction[prop] === 'function') {
        window.myFunction[prop]();
      }
    }
 };

Post a Comment for "How Do I Run This Function On Page Load?"