-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaceholder.js
More file actions
22 lines (18 loc) · 877 Bytes
/
placeholder.js
File metadata and controls
22 lines (18 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Simple script to replace missing images with placeholders
document.addEventListener('DOMContentLoaded', function() {
// Find all images on the page
const images = document.querySelectorAll('img');
// Handle image error events
images.forEach(img => {
img.onerror = function() {
// Get image dimensions
const width = this.getAttribute('width') || this.clientWidth || 300;
const height = this.getAttribute('height') || this.clientHeight || 200;
// Get image alt text or fallback
const altText = this.alt || 'Placeholder Image';
// Set placeholder image
this.src = `https://via.placeholder.com/${width}x${height}?text=${encodeURIComponent(altText)}`;
this.onerror = null; // Prevent infinite error loop
};
});
});