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
Please enter at least three characters to search
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Trending

  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • How to Perform Custom Error Handling With ANTLR
  • Memory Leak Due to Time-Taking finalize() Method
  • Infrastructure as Code (IaC) Beyond the Basics

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
Likes (5)
Comment
Save
Tweet
Share
13.7K 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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!