@fngr/loady

0.1.7 • Public • Published

loady

loady is a tiny and lightweight (1.4kb gzipped) library that loads scripts after a selected event was triggered – for example a click on a button. If the consent is already given, the script will be loaded immediately.

npm version dependencies code style: prettier contributions welcome

Installation

Install via npm

$ npm i @fngr/loady

Usage

import loady from '@fngr/loady'

loady({
  triggerElement: document.getElementById('myButton'),
  event: 'click',
  storageItem: 'load-chartjs-consent',
  script: 'https://cdn.jsdelivr.net/npm/chart.js@2.8.0',
  attributes: {
    defer: true,
  },
})

Options

triggerElement: DOM Element, required
The DOM Element where the event listener will be added to.

event: String, required, default: 'click'
String of event type that triggers event listener on triggerElement

storageItem: String, required
Key of the item that will be placed in localStorage to determine consent

script: String, required
The URL of the script to be loaded after consent

attributes: Object, optional
Object of attributes that should be added to script tag. Something like { 'defer': true }

License

MIT

Package Sidebar

Install

npm i @fngr/loady

Weekly Downloads

1

Version

0.1.7

License

MIT

Unpacked Size

7.51 kB

Total Files

6

Last publish

Collaborators

  • fngr