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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • Batching Strategies: Throughput vs Latency in Practice
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems
  • PII Detection Before RAG Ingestion: Screening Documents in C#

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Avoid Loading JS Files Multiple Times for Component Based Web Frameworks

Get more efficient with loading files in JavaScript.

By 
Yilmaz Bahcetepe
·
Updated Oct. 31, 19 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook