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

  • SEARCH
  • Home
  • 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 6698835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:37:29+00:00 2026-05-26T06:37:29+00:00

Currently I am using a single .js file for a whole project (plus included

  • 0

Currently I am using a single .js file for a whole project (plus included libraries). Only occasionally I split the file into multiple files (i.e. front vs back end sections). In the file(s) I attach events (and other functionality) inside of a single jQuery ready event handler using jQuery selectors:

$(document).ready(function() {

   $('#an_element_on_homepage').click(function() {
      // do something
   });

   // ...
   // A lot of similar code here
   // ...

   $('.elements_on_homepage_and_contact_page').click(function() {
      // do something
   });
});

This is perfectly fine and working, but on a larger project there could be a lot of code executing unnecessarily as some events are needed only at specific pages or there could be id/class collisions between different pages. What is the best practice to avoid the problems and still preserve easy maintainability of the JavaScript and HTML code?

I can think of 2 solutions:

  1. Split .js file into multiple files and on every page choose only the files needed. However, it could be hard to distribute the event attachments properly and could cause problems in caching the scripts on client side.

  2. Wrap the event attachments in functions and call them from a HTML code only where needed. Something like:

    function attachClickOnElementOnHomepage() {
       $('#an_element_on_homepage').click(function() {
          // do something
       });
    }
    

    And after in HTML:

    <div id="an_element_on_homepage"></div>
    <script type="text/javascript">attachClickOnElementOnHomepage();<script>
    

    However, I have a feeling this is also not the best solution possible.

Can you think of another/better solutions?

  • 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-05-26T06:37:29+00:00Added an answer on May 26, 2026 at 6:37 am

    Solution 1. is, as you say, not particularly efficient with regards to HTTP roundtrips, although you can probably choose to ignore that concern if you’re making an internal application.

    Solution 2. just looks like a roundabout way of inline event handler registrations.

    The pattern I use is one JS file that has any code I want to reuse as a project-specific “library” – there’s rarely enough of it to warrant splitting it up. And one JS file per unit of server-side code – MVC controller, stand-alone Wicket control – that implements only page-specific behaviour, or wires up to the reusable code. This file is usually named after the controller.

    The advantages:

    • There’s clear association of what JS behaviours happen where
    • You get rid of inline Javascript in pages – I dislike it if only because it makes setting JS breakpoints a chore
    • If you combine your JS libraries, you only include 2-3 script files on any given page, of which only one isn’t reused on every single page
    • Less risk of conflicts where a selector that’s too broad could match elements on a different page than it was intended to and attach unintended behaviour to them.
    • The JS code is still reasonably straightforward – scripts included on pages. No need to devise a module structure.

    A possible downside is that some code may be repeated between the page-specific scripts, but that’s the cost of separating mechanism from policy. This mostly happens to me for trivial code like setting up jQuery UI buttons or Chosen selectboxes; since they share a common theme, my solution was to roll these up into a single JS file for “ui enhancements”.

    You could also look into tooling that will combine fine-grained maintainable javascripts into easier to load ones as a build step; for a large project where the performance gains matter, the effort in maintaining this might not be overkill. The specifics would depend on what your other tooling is, a fallback solution could be as simple as a shellscript that calls the command-line version of jsmin.

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

Sidebar

Related Questions

I'm currently using the default cookies as my single sign on (SSO) but some
I'm currently using a github repository with a single branch, based of a master
I'm currently using VS2005 Profesional and .NET 2.0, and since our project is rather
I'm performing I/O to a single file from multiple threads. Access to this shared
We are using sp's to convert a uploaded file into corresponding master values and
I've recently made a change to a resource file (.rc) in my project using
I would like a little confirmation that I'm doing this correctly. Using rails single
Currently using System.Web.UI.WebControls.FileUpload wrapped in our own control. We have licenses for Telerik. I
Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted
Currently using Google Analytics as a supplement to our paid tracking software, but neither

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.