yepnope - Documentation

What is yepnope?

Yepnope is a small, fast, and versatile JavaScript library designed to load other JavaScript files and CSS files asynchronously. It offers a simple and efficient way to include resources only when needed, improving the performance of your web application by avoiding unnecessary downloads and execution. Yepnope cleverly handles both success and failure callbacks, allowing for robust error handling and graceful degradation. It’s particularly useful for handling conditional loading of scripts based on browser capabilities or user preferences.

Why use yepnope?

Using yepnope offers several advantages:

Basic usage example

The core of yepnope’s functionality revolves around a single function call: yepnope(). It accepts an array of objects, each specifying a resource to load. Each object can include url (required), success, error, and complete callbacks.

yepnope([
  {
    url: "my-script.js",
    success: function() {
      console.log("my-script.js loaded successfully!");
    },
    error: function() {
      console.error("Failed to load my-script.js");
    }
  },
  {
    url: "my-stylesheet.css",
    type: "css" //Specify type for CSS
  }
]);

This example loads my-script.js and my-stylesheet.css. The success callback for the JavaScript file will execute once the script loads and executes successfully, while the error callback will execute if the load fails. The type: "css" property tells yepnope that it’s loading a stylesheet.

Comparison with other loaders

Yepnope, while powerful and versatile for its size, is a simpler loader compared to more complex solutions like RequireJS or Webpack. While those tools offer sophisticated dependency management and module systems, yepnope focuses on straightforward asynchronous loading of individual scripts and stylesheets. This makes it a great choice for projects where the overhead of a full module loader is unnecessary. If you need advanced features like dependency management and AMD/CommonJS module support, RequireJS or Webpack are better suited. However, for simple asynchronous loading, yepnope offers a lightweight and efficient alternative.

Core Functionality

Loading JavaScript files

Loading JavaScript files with yepnope is straightforward. You provide the URL of the JavaScript file within an object in the array passed to the yepnope() function. Yepnope automatically detects that it’s a JavaScript file and handles the loading process.

yepnope([
  { url: "my-script.js" }
]);

This will load my-script.js asynchronously. Note that the script will execute automatically upon successful loading.

Loading CSS files

To load CSS files, specify the url and explicitly set the type property to "css".

yepnope([
  { url: "my-styles.css", type: "css" }
]);

This will load my-styles.css asynchronously. Yepnope handles the appropriate methods for loading and applying stylesheets.

Conditional loading

You can use JavaScript expressions to conditionally load resources. The URL can be a function that returns the URL of the resource based on the current environment or conditions.

yepnope([
  {
    url: function() {
      if (supportsTouch) {
        return "touch-events.js";
      } else {
        return "mouse-events.js";
      }
    }
  }
]);

This example loads either touch-events.js or mouse-events.js based on the value of the supportsTouch variable. Remember to define supportsTouch appropriately before calling yepnope().

Handling errors

Yepnope provides error and complete callback functions to handle successful and failed loads. The error callback function is executed if the resource fails to load for any reason (e.g., 404 error, network issues). The complete callback executes regardless of success or failure.

yepnope([
  {
    url: "my-script.js",
    error: function() {
      console.error("Failed to load my-script.js");
      // Implement fallback mechanism
    },
    complete: function() {
      console.log("Loading of my-script.js is complete.");
    }
  }
]);

Callback functions

The success, error, and complete callback functions are optional but highly recommended for robust error handling and progress tracking. The success callback executes only when a resource loads and executes successfully. Its signature is function() {}. The error and complete callbacks also have the signature function() {}.

Asynchronous loading

Yepnope’s primary feature is asynchronous loading. This means that the loading of resources happens in the background without blocking the execution of the rest of the page. This is crucial for improving the perceived performance of your web application, especially when loading multiple resources. The browser can continue rendering and processing user interactions while yepnope loads the requested files. The callback functions are executed only after the respective resources have finished loading and (in the case of success) have been successfully parsed and executed (for JavaScript) or applied (for CSS).

Advanced Usage

Using yepnope with different build systems

Yepnope’s simplicity makes it easy to integrate into various build systems. Since it’s a small, self-contained library, you can directly include it in your project and use it without requiring significant changes to your build process. You can include it in your HTML directly via a <script> tag or manage it as part of your asset pipeline within tools like Grunt, Gulp, or Webpack. No specific plugins or configurations are generally needed beyond standard asset management procedures.

Implementing custom test conditions

You can enhance conditional loading by creating your own custom test conditions. Instead of relying on built-in browser detection or feature checks, you can implement functions that perform more specific or complex checks.

function isHighResolution() {
  return window.devicePixelRatio > 2;
}

yepnope([
  {
    url: function() {
      return isHighResolution() ? "high-res-images.js" : "low-res-images.js";
    }
  }
]);

This example demonstrates a custom isHighResolution function determining which script to load based on screen resolution.

Preloading resources

While yepnope primarily handles on-demand loading, you can use it to preload resources that might be needed later, improving responsiveness when the resources are actually requested. Simply include them in your yepnope() call, even if you don’t immediately use them. The browser will start downloading them in the background.

yepnope([
  { url: "expensive-calculation.js" }, // Preload, but don't necessarily use immediately.
  { url: "my-script.js", success: function(){/* Use expensive-calculation.js here */} }
]);

Optimizing for performance

Debugging and troubleshooting

If you encounter problems, check your console for error messages. Ensure your URLs are correct and that the resources exist at the specified locations. Use your browser’s developer tools to inspect network activity and confirm that resources are loading successfully. The error and complete callbacks in yepnope can help pinpoint issues. If using conditional loading, carefully verify the truthiness of your conditions. If problems persist, try temporarily removing conditional loading to isolate potential issues with your conditional logic. Carefully check for any syntax errors in your callback functions or in the conditional logic.

API Reference

yepnope() function parameters

The core of yepnope is its single function, yepnope(). It accepts a single argument: an array of objects. Each object represents a resource to be loaded and can have the following properties:

Example:

yepnope([
  { url: "script1.js", success: function() { console.log("script1 loaded"); } },
  { url: "styles.css", type: "css" },
  { url: function(){ return (isMobile) ? "mobile.js" : "desktop.js"; } }
]);

Test functions

Yepnope doesn’t directly include built-in test functions beyond basic type detection (“js” or “css”). Conditional loading is typically implemented by creating custom test functions that evaluate browser capabilities or other conditions, as shown in the examples above. You’ll define these functions yourself based on your application’s needs. They should return a boolean value representing whether or not the condition is met.

Event handling

Yepnope uses the success, error, and complete callbacks within each resource object to handle events. These callbacks provide a mechanism to respond to successful loading, failed loading, and the completion of the loading process (regardless of success or failure). There aren’t any additional dedicated event listeners or event handling mechanisms exposed by the yepnope API itself.

Available plugins and extensions

Yepnope itself is a relatively small and self-contained library. It does not have a formal plugin architecture or a set of officially supported extensions. Its simplicity is its strength. If you need more advanced features like dependency management, you should consider using a more full-featured module loader like RequireJS or Webpack instead. While community-created extensions or modifications might exist, they are not officially supported by the yepnope project.

Examples

Simple script loading

This example demonstrates loading a single JavaScript file:

<!DOCTYPE html>
<html>
<head>
  <title>Yepnope Example</title>
</head>
<body>

<script src="yepnope.1.5.4.js"></script>
<script>
  yepnope({
    url: 'my-script.js',
    success: function() {
      console.log('my-script.js loaded successfully!');
    },
    error: function() {
      console.error('Failed to load my-script.js!');
    }
  });
</script>

</body>
</html>

Remember to replace 'yepnope.1.5.4.js' with the actual path to the yepnope library and 'my-script.js' with the path to your script.

Conditional script loading based on browser capabilities

This example loads different scripts based on whether the browser supports touch events:

function supportsTouch() {
  return ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch;
}

yepnope([
  {
    url: function() {
      return supportsTouch() ? 'touch-script.js' : 'mouse-script.js';
    }
  }
]);

This will load touch-script.js if the browser supports touch events and mouse-script.js otherwise.

Loading multiple resources

This example loads multiple JavaScript and CSS files:

yepnope([
  { url: 'script1.js' },
  { url: 'script2.js' },
  { url: 'styles.css', type: 'css' },
  { url: 'another-script.js', success: function() { alert("Another script loaded!"); } }
]);

Yepnope will load these resources asynchronously. Note that the order of execution is not guaranteed.

Integrating with other libraries

Integrating yepnope with other libraries is generally straightforward. You load other libraries using yepnope, ensuring that any dependencies are resolved before using the library. This might involve using conditional loading or callbacks to ensure proper sequencing. For example, to load jQuery and then use it:

yepnope([
    {url: 'jquery.js',
     success: function() {
         // jQuery is now loaded, use it here
         $(document).ready(function(){
            //Your jQuery code here
         });
     }
    }
]);

Handling asynchronous dependencies

Yepnope doesn’t have built-in dependency management. For complex dependencies, you’ll need to manage the order manually using callbacks. For instance, if script2.js depends on script1.js, ensure script2.js is loaded only after script1.js is successfully loaded:

yepnope([
  {
    url: 'script1.js',
    success: function() {
      yepnope({ url: 'script2.js' });
    },
    error: function() {
      console.error('Failed to load script1.js');
    }
  }
]);

This approach ensures that script2.js won’t attempt to use functionality from script1.js before script1.js has finished loading and execution. For more complex dependency scenarios, consider a more full-featured module loader.

Troubleshooting and FAQs

Common errors and solutions

Frequently asked questions

Community support and resources

Because yepnope is a relatively simple and mature library, dedicated community support forums or extensive documentation beyond the original project documentation may be limited. Your best resources are usually the original project’s source code (if available), related blog posts, or Stack Overflow for troubleshooting specific issues using the yepnope tag. If you encounter problems that cannot be solved through these resources, you may need to consider alternative loading mechanisms or more robust module loaders.