29

I would like to set an image as nothing. By doing src="" many people say it can cause problems to browsers:

http://www.nczonline.net/blog/2009/11/30/empty-image-src-can-destroy-your-site/

so I am not sure if below can face to the same problem as setting src to empty:

<img id="myImage">

since there's no src attribute on this case.

So If I want to initially set an image to nothing, what's the best I can do?

2
  • because initially there's no image, and when process finished, i put an image depending on the process has passed or failed. Commented Oct 1, 2013 at 22:45
  • Ok, using a div and setting its background-image seems to be a good idea. But what are the differences between using img or div with background-image in terms of performance? in my case there a some images loading at the same time and they can be animated gifs, so img or div? is div back-ground compatible with animated gifs? Commented Oct 1, 2013 at 22:54

5 Answers 5

42

Best solution

Initialise the image as follows: src="//:0" like here: <img id="myImage" src="//:0">

Edit: as per the comment of Alex below, using //:0 apparently can trigger the onError event of the img. So beware of this.

Edit 2: From comment by Drkawashima: As of Chrome 61 src="//:0" no longer seems to trigger the onError event.


Other solution

Alternatively, you can use a very small image until you actually fill the src tag: like this image. With this 'very small image', you would then initialise the tag like so:

<img src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D" width="0" height="0" alt="" />

source


Remove element from DOM

Alternatively, if you simply don't want the element to appear in the DOM, just use some JavaScript:

var myObject = document.getElementById('myObject');
myObject.parentNode.removeChild(myObject);

(as per this answer, using JavaScript's .remove() function is not recommended, due to poor browser support in DOM 4)

Or just use some jQuery:

$("#myObject").remove();

Sign up to request clarification or add additional context in comments.

7 Comments

do not use # as the source. for more info: stackoverflow.com/a/28077004/3841049
Using "//:0" as the src triggers the onError event on the img (at least in Chrome as of version 57)
//:0 clearly saved my life. Did not care about the error, since is for a internal project :) I was setting the attr.src by jQuery but it always placed a display:none!important what ever I did.
As of Chrome 61 src="//:0" no longer seems to trigger the onError event
This shows a broken image in chrome after zooming in and out.
|
24

Using a 1px transparent encoded image is an accepted solution (recommended by CSSTricks)

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="">

PS: Don't be confused later when DevTools shows these as network requests of zero bytes, that's just how DevTools works, all imgs are listed as network requests.

DS: alt="" is included because otherwise, screenreaders will read the src out loud

1 Comment

I upvoted it because it's a transparent image/gif. That is a lot more flexible than the white image supplied on the accepted answer. That's great, thanks! (you could mention it in your answer)
0

Just use a hash symbol #. It's valid value for src attribute. :) https://stackoverflow.com/a/28077004/3841049

1 Comment

please check the link. this method is obsolete.
0

Use this script to set a default image if src is empty or blank

$(document).ready(function () {
   $('img').each(function () {
       if($(this).attr('src')=="") {
          $(this).attr('src', 'Images/download.jpg');
       }
   });
});

Comments

0

The broken image goes away with Chrome when there is no src attribute inside the element.

<html>
<title></title>
<body>
// broken image
<img id='image_id' src=""> 

// does not show a broken image
<img id='image_id'>

</body>
</html>

My solution is to use the the removeAttribute to get ride of the broken image.

<script>
var img_obj = document.getElementById('image_id);

img_obj.removeAttribute('src');
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.