Dynamic Web Resource Loading with ResourceLoader.js
1. Download and include the ResourceLoader script in your HTML document:
<script src="path/to/ResourceLoader.js"></script>
2. Handle loading of JavaScript, CSS, images, JSON, and other file types using the include(path, options) method. You can then use the .then() and .catch() methods handle success and failure.
// JavaScript & CSS
ResourceLoader.include(
[
'/path/to/js.js',
'/path/to/css.css',
],
{
// options here
}
)
.then(() => {
console.log('Loaded');
})
.catch((error) => {
console.error('Error:', error);
}); // JSON Data
ResourceLoader.include(['/path/to/data.json'], {
onSuccess: (data) => {
console.log('Loaded:', data);
},
onError: (error, url) => {
console.error(`Error loading JSON from: ${url}`, error.message);
},
}); // Image
ResourceLoader.include(['/path/to/image.jpg'], {
onSuccess: (url) => {
const img = new Image();
img.src = url;
console.log('Loaded');
},
onError: (error, url) => {
console.error(`Error loading image from: ${url}`, error.message);
},
}); // load other file types as blob
ResourceLoader.include(['/path/to/audio.mp3'], {
onSuccess: (data) => {
const blobUrl = URL.createObjectURL(data);
const audioElement = document.createElement('audio');
audioElement.controls = true;
audioElement.src = blobUrl;
},
onError: (error, url) => {
console.error(`Error loading audio from: ${url}`, error.message);
},
}); 3. Customize how resources are loaded using the following options:
4. API methods.
// Unloads a resource from the page.
ResourceLoader.unloadResource('/path/to/file/');
// Cancels the loading of a resource.
ResourceLoader.cancelResource('/path/to/file/');
// Cancels all pending resource loads
ResourceLoader.cancelAll();
// Gets the current state of a resource
const state = ResourceLoader.getResourceState('https://example.com/script.js'); 03/14/2026
The post Dynamic Web Resource Loading with ResourceLoader.js appeared first on CSS Script.
Jostling a folded piece of paper, holding it marooned in the air, selectman Beth Blair…
Boscawen voters cruised through a speedy town meeting Friday night, one with so little controversy…
Happy Saturday, all! This week, we found a number of deals that should help you…
Though it was weird to see the Golden Globes partner with Polymarket for its most…
Neo to the left of me. Pros are to the right. | Photo: Antonio G.…
Zendesk is to acquire Forethought AI. It says that this will be its largest acquisition…
This website uses cookies.