npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

load-js

v3.1.1

Published

Promise based script loader for the browser using script tags

Downloads

14,120

Readme

load-js

Greenkeeper badge

Promise based script loader for the browser using script tags.

This is a UMD module, so feel free to include it in your nodejs bundling pipeline or directly in the browser via script tags.

Promise implementation needs to be polyfilled if environment does not already provide it.

usage

install

$ npm install load-js

api

loadJS is a method that loads scripts concurrently using script tags. loadJS takes in a single or an array of items where the items can be just a url string, or a config object with options for configuring:

  • type: defaults to text/javascript.
  • async: defaults to false.
  • charset: defaults to utf-8.
  • id: no default value.
  • url: Location of the script to load. Required if no text is provided.
  • text: Script text to execute. Required if no url is provided.
  • cache: flag to determine if item with ID or URL is to be cached. defaults to true.
  • allowExternal: flag to handle situations when the DOM already has a script element with the same ID or URL as what loadJS is being told to load. By default, loadJS will use script elements that already exist in the DOM. To turn off this behavior, set allowExternal to false.
  • debug: flag to show debug message. defaults to false.

Some of these options are described in detail here.

text and url are mutually exclusive and you must specify one. If you call loadJS with a string as a parameter that string will be treated as a url. If you specify both, then url will be used.

The async flag will enable the browsers ability load and execute scripts as soon as possible. This means that scripts are likely going to excute out of order. Because of the nondeterministic script execution nature of async, it is defaulted to false.

The elaborate more on the allowExternal flag. As explained in the options, this flag is particularly useful for handling situations when the DOM already has script elements with the same ID or URL as what loadJS is supposed to load. For example, if a script element with a URL https://awesome.cdn/react.js already exists in the DOM, and for some reason you ask loadJS to load that same URL, loadJS by default will return what already exists in the DOM instead of loading a new script. In order for loadJS to ignore what's already in the DOM and load its own script you need to set allowExternal to false.

examples

Let's just give a simple example where load-js is loaded via a script tag in you HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <!-- Include load-js -->
    <script type="text/javascript" src="https://unpkg.com/[email protected]"></script>

    <script type="text/javascript">
      /* load your stuff */
      loadJS(["https://code.jquery.com/jquery-2.2.1.js", "https://unpkg.com/[email protected]/dist/react.min.js"])
        .then(function() {
          console.log("jQuery and react are loaded");
        });
    </script>
  </head>

  <body>
  </body>
</html>

Another example configuring the script execution to be asynchonous

loadJS([{
  async: true,
  url: "https://code.jquery.com/jquery-2.2.1.js"
}, {
  async: true,
  url: "https://unpkg.com/[email protected]/dist/react.min.js"
}])
.then(() => {
  console.log("all done!");
});