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.
Install via npm
$ npm i @fngr/loady
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,
},
})
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 }
MIT