Check for existence of an image at a given URL with JavaScript/jQuery
This post will discuss how to check for the existence of an image at a given URL in JavaScript and jQuery.
1. Using jQuery
To check for the existence of an image with jQuery, you can simply perform an asynchronous HTTP (Ajax) request.
This can be done using the jQuery.ajax() function. You can either call the jQuery.ajax() method to load data from the server, or use other simpler alternatives to fetch data from the server like jQuery.get() and .load(), which uses jQuery.ajax() behind the scenes.
⮚ Using jQuery.ajax()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var url = 'https://secure.gravatar.com/avatar?d=wavatar'; $(document).ready(function() { $.ajax({ url: url, type: 'HEAD', success: function() { alert('success'); }, error: function() { alert('failure'); } }); }); |
⮚ Using .load()
|
1 2 3 4 5 6 7 8 9 10 11 12 |
var url = 'https://secure.gravatar.com/avatar?d=wavatar'; $(document).ready(function() { $('<img>').load(url, function(response, status) { if (status === 'success') { alert('success'); } else { alert('failure'); } }); }); |
⮚ Using jQuery.get()
|
1 2 3 4 5 6 7 8 9 10 |
var url = 'https://secure.gravatar.com/avatar?d=wavatar'; $(document).ready(function() { $.get(url, function() { alert('success'); }) .fail(function() { alert('failure'); }) }); |
Alternatively, you can dynamically create an image element and attach the onload and onerror event handler using the .on() method.
|
1 2 3 4 5 6 7 8 |
var url = 'https://secure.gravatar.com/avatar?d=wavatar'; $(document).ready(function() { $("<img>") .attr('src', url) .on('load', () => alert('success')) .on('error', () => alert('failure')); }); |
2. Using JavaScript
In plain JavaScript, you can use the powerful Fetch API fetch() method to fetch a resource.
|
1 2 3 4 5 6 7 8 9 10 11 |
var url = 'https://secure.gravatar.com/avatar?d=wavatar'; window.addEventListener("load", event => { fetch(new Request(url, {method: 'HEAD', mode: 'no-cors'})) .then(function() { alert('success'); }) .catch(function() { alert('failure'); }); }); |
Alternatively, you can use the Image() constructor, and register the onload and onerror event listener with the addEventListener() method.
|
1 2 3 4 5 6 7 8 9 |
var url = 'https://secure.gravatar.com/avatar?d=wavatar'; window.addEventListener("load", event => { var image = new Image(); image.src = url; image.addEventListener('load', () => alert('success')); image.addEventListener('error', () => alert('failure')); }); |
3. In HTML
If you don’t want to use JavaScript, you can use the onload and onerror attribute of an img element. The onload attribute fires when the image has been loaded, while the onerror attribute fires if an error occurs while loading an image.
|
1 2 3 |
<img src="https://secure.gravatar.com/avatar?d=wavatar" onload="javascript: alert('success')" onerror="javascript: alert('failure')" /> |
That’s all about checking for the existence of an image at a given URL in JavaScript and jQuery.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)