Last updated: Mar 5, 2024
Reading timeยท4 min
To check if an img src is valid:
error
event listener on the img
element.src
is invalid, set it to a backup image.Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <img id="img" src="https://example.com/does-not-exist.jpg" alt="banner" /> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const img = document.getElementById('img'); img.addEventListener('error', function handleError() { console.log(img.src); // ๐๏ธ if set to non-existent image, causes infinite loop // img.src = 'backup.webp'; // ๐๏ธ must be a valid image });
We set an error event listener on the image element.
When an image's source fails to load, an error
event is fired at the element
that initiated the load.
When the error
event is fired, the handleError
function gets invoked.
You could set the image's src
property to a backup image or hide the image.
src
property to an invalid image, it will trigger the error
event again and cause an infinite loop.An alternative approach is to hide the image if it fails to load.
img.addEventListener('error', function handleError() { console.log(img.src); img.style.display = 'none'; });
In this example, we set the image's
display property to none
, which hides
the image.
Alternatively, you can set the image's
visibility
property to hidden
, which makes the image invisible but it still takes up
space on the page.
const img = document.getElementById('img'); img.addEventListener('error', function handleError() { console.log(img.src); img.style.visibility = 'hidden'; });
When an element's visibility
property is set to hidden
, it still takes up
space on the page, however, the element is invisible (not drawn). It still
affects the layout of your page as normal.
Use the getAttribute()
method to check if an image src is empty.
If the src
attribute doesn't exist, the method returns either null
or an
empty string, depending on the browser's implementation.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <img id="img" alt="banner" /> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const img = document.getElementById('img'); const src = img.getAttribute('src'); if (!src) { console.log('img src is empty'); } else { console.log('img src is NOT empty'); }
We used the
getAttribute()
method to get the value of the src
attribute of the image element.
If the provided attribute is not set on the element, the method returns a null
or ""
(empty string) value.
If the element's src
attribute is not empty, the value of the attribute is
returned, here is an example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <img id="img" src="http://bobbyhadz.com/images/blog/javascript-replace-all-child-nodes/banner.webp" alt="banner" /> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const img = document.getElementById('img'); const src = img.getAttribute('src'); console.log(src); // ๐๏ธ "http://bobbyhadz.com/images/blog/javascript-replace-all-child-nodes/banner.webp" if (!src) { console.log('img src is empty'); } else { // ๐๏ธ this runs console.log('img src is NOT empty'); }
If the src
attribute is set on the image element, the else
block runs.
src
attribute of multiple images is emptyYou can use the same approach to check if the src
attribute of multiple images
is empty.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <img id="img1" alt="banner" /> <img id="img2" alt="banner" /> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const img1 = document.getElementById('img1'); const src1 = img1.getAttribute('src'); const img2 = document.getElementById('img2'); const src2 = img2.getAttribute('src'); if (!src1 && !src2) { console.log('The src attributes of both images are empty'); } else { console.log('The src attribute of at least 1 image is set'); }
We used the logical AND (&&) operator so for the if
block to run both
conditions have to be met.
If the src
attribute of both images is empty, the if
block runs, otherwise,
the else
block runs.
You can use the logical OR (||) operator to check if the src
attribute of at
least one image is empty.
const img1 = document.getElementById('img1'); const src1 = img1.getAttribute('src'); const img2 = document.getElementById('img2'); const src2 = img2.getAttribute('src'); if (!src1 || !src2) { console.log('The src attribute of at least 1 image is empty'); } else { console.log('The src attribute of the images is set'); }
The if
block is run if the src
attribute of at least 1 image is empty,
otherwise, the else
block runs.
You could also explicitly check if the src
attribute of the image is set to
null
or an empty string to check if it's empty.
const img = document.getElementById('img'); const src = img.getAttribute('src'); console.log(src); // ๐๏ธ null if (src === null || src === '') { console.log('img src is empty'); } else { console.log('img src is NOT empty'); }
We used the logical OR (||) operator, so the if
block is run if either
condition is met.
Note that the src
attribute of the image might be set to either null
or
empty string if the src
attribute of the image is empty.
The value is not unified and is browser implementation dependent.
This is why checking if the property is falsy is a much better option.
const img = document.getElementById('img'); const src = img.getAttribute('src'); if (!src) { console.log('img src is empty'); } else { console.log('img src is NOT empty'); }
The if
block will run if the src
property is null
or an empty string.