Skip to main content
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: Andy Matthews and Doug Hughes
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: Andy Matthews ( @commadelimited ) Doug Hughes ( @doughughes )

Testing IMG Complete With No SRC Attribute

Published in Comments (10)

When it comes to IMG DOM (Document Object Model) nodes, you can programmatically test to see if an image has loaded by examining its "complete" property. This is a boolean value that indicates whether or not the image has fully loaded the resource defined by its "src" property. But what about an IMG node that don't have a defined "src" property? Are they complete?

To test this, I put together a tiny demo that looked at IMG objects defined in the DOM tree as well IMG objects defined as detached DOM nodes:

<!doctype html>
		Testing IMG Complete With No Source Attribute

	<!-- IMG tag with no SRC attribute. -->
	<img id="noSrcImage" />

	<!-- Load jQuery from the CDN. -->
	<script type="text/javascript">

		// Get the image from the document.
		var img = document.getElementById( "noSrcImage" );

		// Build an image from scratch.
		var scratch = new Image();

		// Test both images for "completeness."
		console.log( img.complete, scratch.complete );



When I run this code in Firefox, Chrome, and Safari, I get the following output:

true true

However, when I run this code in IE (Internet Explorer), I get the following output:

false false

Why does this not surprise me?

For all the modern browsers, an image with no SRC is complete. Philosophically, I think this makes sense; if there is nothing to load, then the load process is complete. But, according to IE, an image with no SRC is not complete - is not loaded.

For the vast majority of developers, this probably makes no difference one way or the other; but, in an AngularJS context, where you can create custom directives that programmatically interact with IMG SRC attributes and visual state, this kind of scenario becomes a lot more meaningful - but that's another blog post altogether.

Want to use code from this post? Check out the license.

Reader Comments


Aren't SRC and ALT attributes required for the IMG tag?

I've only been able to find the requirement listed here:
but it doesn't appear to be mentioned here:

I copied your sample code and pasted it into the W3C validator and it reported that the IMG tags were invalid:
"Element img is missing required attribute src."
"An img element must have an alt attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images."



That's a good question. I'm not sure how to answer. After all, you can programmatically instantiate an IMG dom node without giving it a SRC properly. So, form a DOM standpoint, I'd say that it's valid. But, from an HTML standpoint, that's another question altogether.

And, like I said in the post, this probably won't matter in the vast majority of cases. I've been doing a lot of AngularJS lately, and have dealt with custom directives that interact with images. Knowing whether an IMG has been loaded can be important for those outliers.



Thank you! I started thinking about this because I'm working on a IMG-related directive in AngularJS. Hopefully more to come soone.


In a phrase I use rarely and with great caution, I tend to agree with IE on this one.

When it comes to IMG DOM (Document Object Model) nodes, you can programmatically test to see if an image has loaded by examining its "complete" property.

If the image cannot load because of an invalid src, then "complete" should never be true. An invalid src is one that does not point to a valid image file.

Realistically, they're both true. Depending on how you look at it. One definition clearly references when the tag has completed rendering and the other references when the image is done rendering. That's why definitions are so important.

<ShamelessPlug>Just this morning, I posted an article on the importance on agreeing on definitions before beginning any work.</ShamelessPlug>



According to the link, SRC is required and ALT is not. On that page, the SRC attribute has a little star next to it. If you hover over that, it says that the attribute is required.



Yeah, I can definitely see it from both sides. As much as no-source can feel like it has loaded, I can also see it like a Object that has been instantiated but not "invoked" yet :)


I am experiencing sort of a dilemma....

I get html fragments and try to remove what some sites call image spacer.
However, your technique is not working because the image they are adding has only the following markup:
<img height="1" alt="" width="1">

Since there are other images that have src and I need to keep, I am not being able to find a solution.

Can you provide some tips?!


I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel