Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:25:59+00:00 2026-06-16T03:25:59+00:00

I have multiple JavaScript files and each of them have each DOMContentLoaded handler in

  • 0

I have multiple JavaScript files and each of them have each DOMContentLoaded handler in order to initialize themselves.

Such as:

file A

document.addEventListener('DOMContentLoaded', function(){
  console.log('init file A');
});

file B

document.addEventListener('DOMContentLoaded', function(){
  console.log('init file B');
});

And I have to concatenate and minify these files, and a minified file have a bunch of DOMContentLoaded handlers.

I am wondering that if it is better to integrate these DOMContentLoaded handlers into one, or not.

I came up with a way of integration such as below.

some common file

window.pageInitializer = {
  initPageFuncs: {},
  do: function(){
    for (var key in this.initPageFuncs) {
      this.initPageFuncs[key]();
    }
  }
}

document.addEventListener('DOMContentLoaded', window.pageInitializer.do);

file A

(function(){
  var initPage = function(){
    console.log('init file A');
  };
  window.pageInitializer.initPageFuncs.fileA = initPage;
})();

file B

(function(){
  var initPage = function(){
    console.log('init file B');
  };
  window.pageInitializer.initPageFuncs.fileB = initPage;
})();

Any help is appreciated,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:26:01+00:00Added an answer on June 16, 2026 at 3:26 am

    IMHO you need not create any such wrappers plainly for performance sake. The only valid complaint the JavaScript developers have with the plain-old event model is management of the event handlers.

    Consider this; every time you add listeners for the DOMContentLoaded event, the browser itself is maintaining a list synonomous to your window.pageInitializer.initPageFuncs object. The browser itself calls on all the event handlers when the event is triggered. You would not want to redo this from a performance point of view or plainly said based on the DRY principle. However managing event handlers is what forces the developers hand to repeat a structure like you have above. What do I mean by managing event handlers? Read on.

    What if, based on a particular program condition or after performing a particular program operation you want to clear all event handlers of the DOMContentLoaded event? In the plain-old approach you will need to call document.removeEventListener for every instance of document.addEventListener that you had called. But if you have the structure that you have created above you can clear all listeners with just the following one line code:

    window.pageInitializer.initPageFuncs = {};
    

    All your listeners are cleared. Doing something like this is one of the main reasons the javascript developer is attracted to jQuery. Remember this?

    var selectedObject = $({selector});
    
    // add event listeners to the selected object
    selectedObject.bind("click", function1);
    selectedObject.bind("click", function2);
    selectedObject.bind("click", function3);
    
    // remove event listeners for the "click" event for the selected object
    selectedObject.unbind("click");
    
    // remove all event listeners for the selected object
    selectedObject.unbind();
    

    From what I see in your question I am assuming you have no need to handle these events and therefore do not require to combine these event bindings into one. If you do and jQuery sounds overkill then you can go with your simple approach to manage your events.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a folder structure that contains multiple javascript files, each of these files
I have multiple javascript functions in my html page. One of them, by far
I have a website with multiple pages, each of them with custom event handlers
I've came across this use case: Multiple template files have their own JavaScript code
Working on an AJAX website (HTML,CSS,JavaScript, AJAX, PHP, MySQL). I have multiple javascript functions
I have multiple ajax requests with javascript code as response, and I need to
I am using javascript, google direction service I have multiple source points on the
I have multiple input fields on my page. I Regex them with a css
I have a very simple question....I have multiple jsp files like login.jsp , Registration.jsp
I have a large XML file (converted to JSON) that consists of multiple repeating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.