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 7664133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:13:40+00:00 2026-05-31T14:13:40+00:00

I am looking for best practices/design patterns for keeping JavaScript in external JS files/bottom

  • 0

I am looking for best practices/design patterns for keeping JavaScript in external JS files/bottom of the page. I am running into a problem where my pages are built from a number of components, and each component replies on a jquery component that needs several params passed to it.

So for instance in a JSP

<div class="component-a">
   <div id="js-slideshow-<%= slide-id %>" class="js-slideshow"></div>
</div>
<script>$('#js-slideshow-<%= slide-id %>').initslideshow({'path':<%= path %>});</script>

The problem is I now have tags peppering my markup and there is alot of repeat code. Ideally I would break out all these JS calls into a JS file that is included at the bottom of the page.

My initial thought would be to load up any params the jquery plugins need as data-* attributes.

Something like

JSP

<div class="component-a">
   <div data-path="<%= path %>" class="js-slideshow"></div>
</div>

JS file

<script>
$(".js-slideshow").each(function() { 
   $(this).initslideshow('path': $(this).attr('data-path'));
});
</script>

Are there any other good patterns for keeping JavaScript separated out in JS files even when the calls require data/context the server needs to provide and may only be available from the context of a component?


Clarifications:

This is simply practice of breaking out common presentation elements into separate files. Ruby on Rails partials are one example of this.
One component may be a “Slideshow” but depending on which page its located on i may want adjust its behavior using different values, such as different sets of images to show, different size images, or a different title, etc. This “variety” in parameters “known” by the server, and it would be up to the server to understand the context to pull the param values from a serverside data store and write them to the JS calls.

Ideally I would like to minimize the number of times the same “base” JS is written out and execute it at the bottom once for the following reasons:
1. Decrease page weight
2. Increase readability of generated HTML
3. Moved javascript code (even if they are just calls) from the HTML file to a JS file(s) – which is a clean separation of code and data
4. Allows for more data to be cached (the JS file can be cached and the extra page weight doesnt have to be downloaded on other page requests)
5. Avoid the scenario of more complex components having multiple events bound to the same element. For example:

$(".slideshow").init('<%= param %>') adds a bind event to the .slideshow element, if I call $(".slideshow").init('<%= param %>') multiple times, multiple events will be bound to that DOM element. (I think bind works like that, if not live or delegate does).
I can get around it by calling init on a uniquely named element, but this requires adding unique ids to each slideshow DOM element, which seems unnecessary.

In terms of development I will most likely have a separate JS file for each component, but will pack them into a single file during the build process.

  • 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-31T14:13:41+00:00Added an answer on May 31, 2026 at 2:13 pm

    so what you are saying is that the parts of your site come in “modules” that have their own HTML and JS, and that when you sum it all up, they are just in the middle of things?

    i suggest you put script paths as variables or data-* and have a loader at the bottom do the lazy loading later.

    like say:

    <div class="component" data-source="path_to_script">
        your module HTML
    

    then at the bottom of your page, before your `, you have some script that does:

    <script>
        $(function(){
    
            //loader script
            (function loader(){
    
                //loop through all components
                $('.component').each(function(){
    
                    //get the path and load script
                    var scriptUrl = $(this).data('source');
                    $.getScript(scriptUrl);
    
                    //or if params
                    var params = $(this).data('params'); //may be in JSON string perhaps?
                    //parse JSON here
                    $(this).initSlideshow(params);                    
    
                });
            }());
        });
    </script>
    

    in the end, you have a clean page. at least, you need jQuery and the code above in the page. the rest of your scripts are dynamically loaded at the bottom.

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

Sidebar

Related Questions

I am looking for any resources that gives examples of Best Practices, Design patterns
I am looking for some best practices/patterns to transform objects from one to another.
I am looking for some design best practice or heuristic to address performance issues
I'm looking for best practices here. Sorry. I know it's subjective, but there are
I am using the NHibernate.Search assembly and am looking for best practices around using
I am looking for ways / best practices on testing methods defined in an
Web.Config? Website properties? I'm looking for suggestions/best practices with reasons you recommend them.
Looking for some resources on SOA / BPEL best practices. Specially BPEL.
I’m looking for recommendations and best practices for applying signed assemblies in an organization
I'm looking for a good summary of best practices for working with TFS source

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.