Skip to content

About

A module to load frontend assets.

Topics

Resources

Contributing

Security policy

Stars

3 stars

Watchers

12 watching

Forks

Repository files navigation

asset-loader

Installation

npm install --save @braintree/asset-loader

Usage

var loadScript = require("@braintree/asset-loader/load-script");

loadScript({
  src: "https://example.com/some-js-file.js",
  container: domNodeToAddScriptTagTo, // optional, defaults to document.head
  id: "id-to-give-script-tag", // optional
  crossorigin: "anonymous", // optional
  dataAttributes: {
    // optional
    foo: "value", // adds data-foo="value" to script tag
  },
  // optional, by default a repeated call with the same options reuses the
  // earlier load. Setting this to `true` always adds a new script tag and
  // replaces the reused load for later calls with the same options.
  forceScriptReload: true,
  // optional, milliseconds to wait before giving up on a load that never
  // finishes. There is no timeout by default. The script tag can't be
  // cancelled, so a retry after a slow load may run the script twice.
  timeout: 60000,
})
  .then(function (script) {
    script; // a reference to the dom node
  })
  .catch(function (err) {
    // occurs when the script fails to load, is aborted or times out.
    // err is an AssetLoadError with `src`, `failureKind` ("error", "abort"
    // or "timeout"), `timing` (ms until failure) and `onLine`
    // (navigator.onLine at failure). Calling loadScript again with the
    // same options starts a new load.
  });
var loadStylesheet = require("@braintree/asset-loader/load-stylesheet");

loadStylesheet({
  href: "https://example.com/some-css-file.css",
  container: domNodeToAddScriptTagTo, // optional, defaults to document.head and always puts it at the top of the container
  id: "id-to-give-to-stylesheet-element", // optional
}).then(function (stylesheet) {
  stylesheet; // a reference to the dom node
});

Both methods will resolve immediately if the script or link element is already on the page.

Development

Run tests:

npm test

About

A module to load frontend assets.

Topics

Resources

Contributing

Security policy

Stars

3 stars

Watchers

12 watching

Forks

Releases

Packages

Used by

Contributors

Languages