Forum Moderators: open

Message Too Old, No Replies

Closing an anchor element with />

Seems to be valid, but upsets Safari

         

thesheep

4:39 pm on Mar 8, 2007 (gmt 0)

10+ Year Member



Recently had a site with this at the top:

<a name="content" />

Now I thought that would be invalid, but it seems to validate fine.

However, Safari was not happy and tries to render all content on the entire page as a single link.

So I guess it needs to be changed back to

<a name="content"></a>

So is this right, this is valid code but Safari has a bug?

encyclo

5:18 pm on Mar 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What doctype are you specifying? It is valid XHTML, but unsupported as HTML as it is XML syntax not HTML syntax. It's a good example that shows that the idea that XHTML is compatible with HTML is false. :)

thesheep

10:35 am on Mar 9, 2007 (gmt 0)

10+ Year Member



I'm using XHTML transitional doctype.

If it had been an HTML doctype, I don't think it would have validated.

Anyway, if it is valid XHTML, as you say, looks like it's a Safari bug.

encyclo

11:22 am on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not a Safari bug if you're actually serving XHTML as
text/html
- which you almost certainly are doing as from memory Safari (and Konqueror on which Safari is based) does not support "true" XHTML served as
application/xhtml+xml
.

In other words, Safari is an HTML parser, and you are sending markup which isn't actually HTML, just pretending to be HTML.

The validator is technically correct, but unhelpful (and verging on misleading) as the markup example isn't compatible with HTML.

thesheep

2:03 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



Well it still sort of seems like a Safari bug, as that browser has no problems with code like


<br />

so Safari developers obviously tried to accept some XHTML conventions, so you could sort of see it as a bug not to accept others (unless you're saying that <br /> is valid HTML?)

encyclo

2:58 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Safari is handling both situations in the same way - it is seeing the trailing slash as an unknown attribute (ie. a coding error) and ignoring it. The end result, however, is different because the two elements are different in that
br
does not have a closing tag.

So

<br />
is read as
<br>
- which works.
<a name="content" />
is read as
<a name="content">
- which opens a link up until a subsequent
</a>
(or the end of the page if there is no appropriate closing tag).

The closing slash is invalid HTML, and you are declaring the page is HTML via the mime type. So, Safari is working according to HTML specifications, and the other browsers are using their error-handling routines to fix the malformed HTML. You can consider the other browsers' approach as being superior, but it doesn't make Safari's approach wrong. :)

  • Why most of us should NOT use XHTML [webmasterworld.com]
  • thesheep

    8:27 am on Mar 12, 2007 (gmt 0)

    10+ Year Member



    OK got it, thanks. I learn something every day!