https://github.com/fngr2911/loady
Load scripts after an event like a click on a button
https://github.com/fngr2911/loady
consent-management events javascript load third-party-libraries
Last synced: 6 months ago
JSON representation
Load scripts after an event like a click on a button
- Host: GitHub
- URL: https://github.com/fngr2911/loady
- Owner: FNGR2911
- Created: 2020-04-24T10:03:13.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T04:20:53.000Z (almost 3 years ago)
- Last Synced: 2025-05-19T17:21:44.071Z (7 months ago)
- Topics: consent-management, events, javascript, load, third-party-libraries
- Language: JavaScript
- Size: 896 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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.
[](https://badge.fury.io/js/%40fngr%2Floady)
[](https://david-dm.org/fngr2911/loady)
[](https://github.com/prettier/prettier)
[](https://github.com/fngr2911/loady/issues)
### Installation
Install via npm
```bash
$ npm i @fngr/loady
```
### Usage
```javascript
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