Diego De La Puente

Setting Up the Base JavaScript File

1. Overview

In modern web development, having a single point for loading JavaScript libraries helps maintain consistency and manage dependencies efficiently across a web application. This guide covers setting up a `load-libraries.js` file to streamline JavaScript library loading across your project.

The `load-libraries.js` file acts as a central hub for importing various JavaScript libraries used in the application, making it easier to manage and apply scripts globally.

2. Steps to Create and Configure `load-libraries.js`

Follow these steps to create and set up the `load-libraries.js` file:

  1. Create a new file named load-libraries.js in your project's JavaScript directory.
  2. In load-libraries.js, include scripts using a function to load them dynamically. For example:
    // Load multiple CDN links using the same base code structure
    (function() {
        // Define an array of CDN URLs
        var scriptUrls = [
            'https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js'
            // Add more URLs as needed separated by a comma
        ];
    
        // Function to load a script
        function loadScript(url) {
            var script = document.createElement('script');
            script.src = url;
            script.async = true;
            
            // Add error handling
            script.onerror = function() {
                console.error('Failed to load script:', url);
            };
            
            document.head.appendChild(script);
            // console.log('Script added:', url);  
            // Uncomment above line for testing
            // uncommenting above line will show loaded URL in Dev View
        }
    
        // Iterate over the array and load each script
        scriptUrls.forEach(loadScript);
    })();
    
  3. Ensure each included script handles a specific aspect of your application’s functionality, such as utility libraries, UI components, or analytics.

3. Benefits of Using `load-libraries.js`

Using a central script loader like `load-libraries.js` offers several benefits:

  • Maintainability: Centralizing script loading reduces duplication and makes it easier to manage changes.
  • Organization: Clearly defined sections for different scripts help keep your codebase organized.
  • Scalability: Adding new scripts becomes straightforward; simply add a new entry to the `scriptUrls` array in `load-libraries.js`.

Using `load-libraries.js` in HTML

1. Adding `load-libraries.js` to Your HTML

To include `load-libraries.js` in your HTML file, add a script tag in the head or body section of your HTML document:

<!-- Example HTML head or body -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Web Application</title>
    <!-- Centralized JavaScript Loader -->
    <script src="../../js/load-libraries.js" type="text/javascript"></script>
</head>
<body>
    <!-- Content goes here -->
</body>
</html>

2. Testing Your Setup

To ensure `load-libraries.js` is properly loaded:

  1. Open your HTML file in a web browser.
  2. Use browser developer tools to inspect the loaded scripts and verify that all included libraries are correctly applied.
  3. Make changes to one of the included script URLs or add new ones and observe the updates in the browser to confirm the setup works.

3. Media

JavaScript Configuration Screenshot