DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Trending

  • Long Tests: Saving All App’s Debug Logs and Writing Your Own Logs
  • AI and Rules for Agile Microservices in Minutes
  • Harmonizing AI: Crafting Personalized Song Suggestions
  • Deploying to Heroku With GitLab CI/CD

Avoid Loading JS Files Multiple Times for Component Based Web Frameworks

Get more efficient with loading files in JavaScript.

By 
Yilmaz Bahcetepe user avatar
Yilmaz Bahcetepe
·
Updated Oct. 31, 19 · Code Snippet
Like (5)
Save
Tweet
Share
12.2K Views

Join the DZone community and get the full member experience.

Join For Free

medical-book-with-tabs

In this article I will talk about how to avoid loading the same JavaScript files multiple times for component-based web frameworks like JSF, Wicket, etc. For example, when you have a JSF composite component and it contains a link to an external JS file, including this composite component multiple times to XHTML will lead to multiple links to same external JS file.

Including the same JS for multiple times in an HTML document can be the reason for weird JS behaviors like running the same code multiple times.

You may also like: JavaServer Faces 2.0.

Solution

To overcome this issue, the solution explained below can be added into component's front end code. (For JSF, it is XHTML of the composite component.)

<script>

  try{

    isComponentResourcesAlreadyIncluded; //this variable is flag of whether resources already loaded or not.

  }
  catch(e) {

    if(e.name == "ReferenceError") {

      isComponentResourcesAlreadyIncluded = true;

      //assuming jquery is included.

      //load JS files as per your needs.
      $.ajax({
        async: false,
        url: "/static/js/a.js",
        dataType: "script"
      });

      $.ajax({
        async: false,
        url: "/static/js/b.js",
        dataType: "script",
        cache: true
      });

      $.ajax({
        async: false,
        url: "/static/js/c.js",
        dataType: "script",
        cache: true
      });

      $.ajax({
        async: false,
        url: "/static/js/d.js",
        dataType: "script"
      });

    }
  }

</script>


How It Works

(Assuming a page contains multiple composite components.)

JavaScript's engine executes instructions one-by-one. First, it comes across with the first composite component's JS block (described above) and executes it. It then throws a ReferenceError, since there is no  isComponentResourcesAlreadyIncluded.

Then, it executes the catch block, sets isComponentResourcesAlreadyIncluded to true, and loads defined JS files. After these executions and maybe executing some other codes, if the script comes across the  same composite component's JS block (described above) and executes it. Since isComponentResourcesAlreadyIncluded is defined in first run, the catch block won't be executed. No more multiple load of same JS files!


Further Reading

  • Would You Use JSF for Your Next Project?

Opinions expressed by DZone contributors are their own.

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: