user1457065
user1457065

Reputation:

<img src="#"> Means in HTML?

I do have the following code in my HTML:

<img src="#" alt="image alternative text" />

What does src="#" mean? Because in HTML, I cannot have empty src attribute for an image tag. And if we do not have that attribute, visual studio 2012 will throw a suggestion.

Thanks

Upvotes: 1

Views: 7400

Answers (5)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201548

In src="#", the attribute value is a reference to the start of the document at the current base URL, according to the URL standard, STD 66 aka RFC 3986. You would need rather special arrangements to make that actually work as a reference to an image.

Why anyone would use such an attribute is a different question, and a yet another question is what is the problem that the construct is supposed to address.

Upvotes: 0

gp.
gp.

Reputation: 8225

When you don't want any image to be loaded using src attribute (probably load the image dynamically later), you need to set src empty. But when you do that, browsers still send calls to server.

Browser behavior for empty src (Source: http://developer.yahoo.com/performance/rules.html)

Internet Explorer makes a request to the directory in which the page is located. Safari and Chrome make a request to the actual page itself. Firefox 3 and earlier versions behave the same as Safari and Chrome, but version 3.5 addressed this issue[bug 444931] and no longer sends a request. Opera does not do anything when an empty image src is encountered.

To avoid the unnecessary call to server, instead of using empty src, src="#" could be used which forms a hash url and hash urls are not sent to server.

Let's say base url is : http://mysite.com/myapp/

src="" -> Absolute url: http://mysite.com/myapp/

src="#" -> Absolute url: http://mysite.com/myapp/#

Upvotes: 6

user2864740
user2864740

Reputation: 61875

As with the href attribute of an anchor element, <img src="#"> is roughly equivalent to <img src="thecurrenturl#" ..> (but see the fiddle example for why it's not identical).

As it is written src will never refer to a valid image resource but, presumably, something/someone can change the URL later or otherwise manipulate the element. Since the src attribute is required1, substituting in such a valid "dummy" value appeases tools like the Visual Studio editor.

This fiddle shows the behavior which can be observed by using the DOM src property.


1 "The src attribute must be present, and must contain a valid non-empty URL .."

Upvotes: 1

user19003768
user19003768

Reputation:

Most people use it as a placeholder for links just so that there are no errors when the code compiles. If a programmer gave you some code with the "#" as a placeholder, they probably want you to interchange it with a web URL.

Upvotes: 1

Jace Cotton
Jace Cotton

Reputation: 2014

More than likely just a placeholder subject to change dynamically based off of an event listener (or like @FabricioMatte suggested, a lazy loading technique.) It could also be just a placeholder to bypass any potential errors.

Upvotes: 0

Related Questions