-1

What's the difference between

 <a href="link"><img src="img" /></a>

and

 <img src="img" onclick="self.location.href='link'" /> 

?

The second option will not work without js enabled, what else?

UPD: yep, didn't see wrong quotes.

3 Answers3

0

all on handlers are javascript based and will only work if javascript enabled.

your second statement is wrong though, its supposed to be

<img src="img" onclick="self.location.href='link'" /> 

and both are the same except that one is html and other is javascript.

there are benefits for both versions, but the result is the same.

Banana
  • 7,424
  • 3
  • 22
  • 43
0

From user experience there's no difference between both methods (if you apply cursor: pointer style to javascript method).

However there's significant differences in different areas.

Semantic. On semantic level you can't know what is intention of having javascript associated. You know that when you use anchor element. Then there's a sure that this element's (and it's children) purpose is to navigate somewhere else.

It leads us to second area - SEO. To properly index you page crawler need to know what's a structure of it and how pages are associated. Crawler do not interpret javascript code. It only use HTML code to analyse your website.

Finally there is accessibility. It's directly associated with semantic. Screen readers, for example, differently behave on anchor element and on image element. They simply will not know that this image is a navigation element. So, some part of your user may not notice that there's an anchor.

My advice is not to use javascript in this context. An anchor element is the best choice.

Pawel Uchida-Psztyc
  • 3,735
  • 2
  • 20
  • 41
-3

One difference is that pure html href can not change port number. If you want to link to a different port you must use js.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424