1. @braintree/asset-loader
A module to load frontend assets.
@braintree/asset-loader
Package: @braintree/asset-loader
Created by: braintree
Last modified: Wed, 29 Nov 2023 21:27:04 GMT
Version: 2.0.0
License: MIT
Downloads: 976,596
Repository: https://github.com/braintree/asset-loader

Install

npm install @braintree/asset-loader
yarn add @braintree/asset-loader

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 the module will resolve with an existing script
  // node if a script tag with the same src exists on the page, by setting
  // this value to `true`, it will add the script to the page even if an
  // identical script tag already exists on the page.
  forceScriptReload: true,
})
  .then(function (script) {
    script; // a reference to the dom node
  })
  .catch(function (err) {
    // occurs when script fails to load or is aborted
  });
 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

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code