Context: Currently, Wt (Web Toolkit) will render a WAnchor object as a <span> tag rather than an <a> tag if the link that the anchor should point to, is empty.
However, there are some problems with this approach, notably that it adds unnecessary complexity to CSS styling (when using Twitter Bootstrap, for example).
I’ve been involved in a discussion with one of the Wt developers proposing a patch to change this behavior: http://redmine.emweb.be/issues/1348
He noted that the current behavior was implemented as a result of certain browsers having problems rendering <a> tags without an href attribute (possibly older mobile browsers?).
According to the official HTML specifications, there is no problem with this:
- http://dev.w3.org/html5/spec/single-page.html#the-a-element If the a element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant.
- http://www.w3.org/TR/html401/struct/links.html#edef-A Authors may also create an A element that specifies no anchors, i.e., that doesn’t specify href, name, or id. Values for these attributes may be set at a later time through scripts.
And as far as I am aware…
<a>Link</a> // OK
<a href>Link</a> or <a href="">Link</a> // Undefined? (but common behavior is to navigate to the current URL)
<a href="/items">Link</a> // OK
I also checked the following document with the W3C HTML5 validator:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<a>Example of an anchor with no href attribute.</a>
<a href>Example of an anchor with a null href attribute.</a>
<a href="">Example of an anchor with an empty string href attribute.</a>
<a href="http://www.google.com/">Example of an anchor that links to Google.</a>
</body>
</html>
I received one warning:
Line 9, Column 8: Attribute href without an explicit value seen. The
attribute may be dropped by IE7. <a href>Example of an anchor with a
null href attribute.</a>
So my question is: are there any known situations where the total lack of an href attribute on an <a> tag causes problems in certain browsers? IE? Mobile Safari?
An
aelement without anhrefattribute has always been valid in HTML, and there is no reason to expect any browser to have an issue with this. Such an element is effectively similar tospan, i.e. lacks any semantics. Traditionally<a name="...">...</a>has been used to set up a destination anchor, usually without anhrefattribute, and such elements work across browsers, though it is more modern and recommendable to use theidattribute (on any element).Using
href=""is formally correct, with an empty URL, which is interpreted as same-document reference.Using
hrefwithout any value is invalid according to current HTML specs. By HTML5 drafts, it can be used in HTML serialization and it is equivalent tohref="". There is no guarantee that browsers behave that way.