Skip to content
/ loady Public

Load scripts after an event like a click on a button

Notifications You must be signed in to change notification settings

FNGR2911/loady

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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