https://github.com/lullaby6/load-async.js
JavaScript Library - Load JavaScript and CSS files asynchronously
https://github.com/lullaby6/load-async.js
java-script javascript js js-lib js-library
Last synced: 9 months ago
JSON representation
JavaScript Library - Load JavaScript and CSS files asynchronously
- Host: GitHub
- URL: https://github.com/lullaby6/load-async.js
- Owner: lullaby6
- Created: 2024-08-26T15:19:44.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-06T15:06:43.000Z (over 1 year ago)
- Last Synced: 2025-01-25T13:25:04.803Z (11 months ago)
- Topics: java-script, javascript, js, js-lib, js-library
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# load-async.js
A lightweight JavaScript library for loading JavaScript and CSS files asynchronously. It provides easy-to-use functions to dynamically load scripts and stylesheets, either synchronously or asynchronously, with error handling capabilities.
## Installation
### CDN
```html
```
## Examples
### Load
```js
loadCSS('https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css');
loadJS('https://cdn.jsdelivr.net/npm/toastify-js', () => {
Toastify({
text: "This is a toast",
duration: 3000
}).showToast();
});
```
### Load Async
```js
try {
await loadAsyncCSS('https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css');
await loadAsyncJS('https://cdn.jsdelivr.net/npm/toastify-js');
Toastify({
text: "This is a toast",
duration: 3000
}).showToast();
} catch (error) {
console.error("Error loading Toastify");
}
```
### Load Async With Error
```js
const [link, cssError] = await loadAsyncCSSWithError('https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css');
if (cssError) {
console.error("Error loading Toastify CSS");
return;
}
let [script, jsError] = await loadAsyncJSWithError('https://cdn.jsdelivr.net/npm/toastify-js');
if (jsError) {
console.error("Error loading Toastify JS");
return;
}
Toastify({
text: "This is a toast",
duration: 3000
}).showToast();
```