Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting an image src to empty

Tags:

html

image

src

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?

like image 829
user304602 Avatar asked Oct 01 '13 21:10

user304602


People also ask

Can img src be empty?

Use the getAttribute() method to check if an image src is empty, e.g. img. getAttribute('src') . If the src attribute does not exist, the method returns either null or empty string, depending on the browser's implementation.

What does img src => do?

The <img> src attribute is used to specify the URL of the source image. Syntax: <img src="URL"> Attribute Values: It contains single value URL which specifies the link of source image. There are two types of URL link which are listed below: Absolute URL: It points to another webpage.

How to check if an image SRC is empty in HTML?

Use the getAttribute () method to check if an image src is empty, e.g. img.getAttribute ('src'). If the src attribute does not exist, the method returns either null or empty string, depending on the browser's implementation. Here is the HTML for the examples in this article. And here is the related JavaScript code.

How to set the src attribute to an empty string?

Use the setAttribute () method to set the image's src attribute to an empty string. Alternatively, hide the image element. Here is the HTML for the examples in this article. And here is the related JavaScript code. We used the setAttribute method to set the src attribute of the image element to an empty string.

Is it possible to remove the SRC of an image?

– Naftali Dec 5 '12 at 15:35 An empty string can actually be a valid src for an image, technically speaking it is not "removing" the src. Same as #, it would use the current URL plus the hash.

Is it possible to remove an empty string from an image?

An empty string can actually be a valid src for an image, technically speaking it is not "removing" the src. Same as #, it would use the current URL plus the hash. Real example: lcamtuf.coredump.cx/squirrelYou would end up sending a request to the current URL. I would suggest using another approach.


3 Answers

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();

like image 158
Jean-Paul Avatar answered Oct 21 '22 18:10

Jean-Paul


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

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

Note that I include an alt set to an empty string, this is intentional to make screenreaders skip this empty image, but if you don't include the alt attribute some screenreaders will read the src value out loud.

PS: You might be confused later when you use DevTools and see these images showing up as 0 Byte Network requests, but don't be alarmed - that's just how DevTools works.

like image 22
Drkawashima Avatar answered Oct 21 '22 18:10

Drkawashima


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

like image 1
iGidas Avatar answered Oct 21 '22 16:10

iGidas