Is an empty href valid?

192,287

Solution 1

Although this question is already answered (tl;dr: yes, an empty href value is valid), none of the existing answers references the relevant specifications.

An empty string can’t be a URI. However, the href attribute doesn’t only take URIs as value, but also URI references. An empty string may be a URI reference.

HTML 4.01

HTML 4.01 uses RFC 2396, where it says in section 4.2. Same-document References (bold emphasis mine):

A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document, and a reference containing only a fragment identifier is a reference to the identified fragment of that document.

RFC 2396 is obsoleted by RFC 3986 (which is currently IETF’s URI standard), which essentially says the same.

HTML5

HTML5 uses (valid URL potentially surrounded by spacesvalid URL) W3C’s URL spec, which has been discontinued. WHATWG’s URL Standard should be used instead (see the last section).

HTML 5.1

HTML 5.1 uses (valid URL potentially surrounded by spacesvalid URL) WHATWG’s URL Standard (see the next section).

WHATWG HTML

WHATWG’s HTML uses (valid URL potentially surrounded by spaces) the definition of valid URL string from WHATWG’s URL Standard, where it says that it can be a relative-URL-with-fragment string, which must at least be a relative-URL string, which can be a path-relative-scheme-less-URL string, which is a path-relative-URL string that doesn’t start with a scheme string followed by :, and its definition says (bold emphasis mine):

A path-relative-URL string must be zero or more URL-path-segment strings, separated from each other by U+002F (/), and not start with U+002F (/).

Solution 2

It is valid.

However, standard practice is to use href="#" or sometimes href="javascript:;".

Solution 3

As others have said, it is valid.

There are some downsides to each approach though:

href="#" adds an extra entry to the browser history (which is annoying when e.g. back-buttoning).

href="" reloads the page

href="javascript:;" does not seem to have any problems (other than looking messy and meaningless) - anyone know of any?

Solution 4

While it may be completely valid HTML to not include an href, especially with an onclick handler, there are some things to consider: it will not be keyboard-focusable without having a tabindex value set. Furthermore, this will be inaccessible to screenreader software using Internet Explorer, as IE will report through the accessibility interfaces that any anchor element without an href attribute as not-focusable, regardless of whether the tabindex has been set.

So while the following may be completely valid:

<a class="arrow">Link content</a>

It's far better to explicitly add a null-effect href attribute

<a href="javascript:void(0);" class="arrow">Link content</a>

For full support of all users, if you're using the class with CSS to render an image, you should also include some text content, such as the title attribute to provide a textual description of what's going on.

<a href="javascript:void(0);" class="arrow" title="Go to linked content">Link content</a>

Solution 5

The current HTML5 draft also allows ommitting the href attribute completely.

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.

To answer your question: Yes it's valid.

Share:
192,287

Related videos on Youtube

matthew
Author by

matthew

Updated on May 31, 2020

Comments

  • matthew
    matthew almost 4 years

    One of our web developers uses the following html as a placeholder for styling a drop down list.

    <a href="" class="arrow"></a>
    

    Is this considered anchor tag valid?

    Since there is no href value, it shows up as broken on some of our link checker reports.

    • ZhongYu
      ZhongYu about 10 years
      It does not work in IE though! Well it works in IE but with a completely different behavior, against the spec. So it is valid per spec, but not in practice :(((
    • Nate
      Nate almost 9 years
      Expanding on what bayou.io said, IE links to the directory above the document: stackoverflow.com/questions/7966791/…
    • Ringo
      Ringo over 7 years
      Yes, an empty href in older versions of IE (7/8, etc.) could have bad consequences, such as directory listing. There is much documented on the subject if you google it
    • Ringo
      Ringo over 7 years
      for example: gtmetrix.com/avoid-empty-src-or-href.html. Never mind just old browsers, it affects current IE, Edge, and also Webkit browsers.
  • Programmer97
    Programmer97 about 13 years
    As Stated in RFC 2396: A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document,
  • diachedelic
    diachedelic over 11 years
    Another interesting one is href="//:0", which will not make a request to the server nor leave any history.
  • David Grenier
    David Grenier over 11 years
    The //:0 makes some of my images fail to load in Chrome. Seems Chrome interprets that as receiving a cancel command.
  • Deckard
    Deckard over 11 years
    when I click href="#", focus moves to top of the page in IE, so I thkink href="javascript:;" is better
  • c24w
    c24w about 11 years
    Although: <a href></a>Attribute href without an explicit value seen. The attribute may be dropped by IE7.
  • tosh
    tosh almost 11 years
    href="#" is an anti-pattern. It adds an extra entry to the browser history and in some cases makes the browser scroll to top, you can just leave out the href attribute if you are not using it. it will default to "" and therefore reload the page if not prevented.
  • dumazy
    dumazy over 10 years
    href:"javascript:;" was just what I needed to make stuff work in both Android an iOS webviews
  • rinat.io
    rinat.io almost 10 years
    I like this solution too. Do you know of any other disadvantages except IE7 behavior?
  • Gui Prá
    Gui Prá over 9 years
    @tosh, no, I'm pretty sure if you leave out the href attribute, the <a> element isn't styled as a link, isn't focusable, and doesn't create a link as <a href=""> or <a href> does. If it does work for you, could you share a JSFiddle showing it?
  • tosh
    tosh over 9 years
    @n2liquid-GuilhermeVieira thanks a lot for your precision. I did not mean that omitting the href attribute is the solution. I did not word it right. I meant to use the empty string as the href attribute value.
  • Gui Prá
    Gui Prá over 9 years
    @tosh, you know what's funny? Apparently since HTML5, omitting the attribute should work that way, see w3.org/TR/html5/text-level-semantics.html#the-a-element (Thanks halfdan for submitting that answer below.) Apparently this hasn't been implemented by vendors yet, and since it's still just candidate recommendation, it's hard to tell whether it'll be kept in the final standard or whether vendors will comply before that.
  • Alex from Jitbit
    Alex from Jitbit about 9 years
    yes, BUT - an <a> element with no href is styled dirrefently in some browsers. For example, ":hover" styles do not work in Chrome (and other weird things)
  • Nate
    Nate almost 9 years
    IE links to the directory above the document: stackoverflow.com/questions/7966791/…
  • Nate
    Nate almost 9 years
    IE links to the directory above the document: stackoverflow.com/questions/7966791/…
  • Kobi
    Kobi over 8 years
    An <a> tag without a href represents something else, not a link (traditionally an anchor). Also, there is a difference between an optional attribute and an attribute that can be empty.
  • Mark Good
    Mark Good over 8 years
    I have not tested this, but I suspect that href="javascript:;" would violate the Content Security Policy that disallows inline JavaScript. If you intend to turn on this policy, I would use one of the alternatives.
  • Daniel Sokolowski
    Daniel Sokolowski about 8 years
    Thanks for mentioning that omitting href will mess with accessibility interfaces.
  • aij
    aij about 8 years
    Indeed. Unfortunately, as you quoted,leaving off the href makes it not a hyperlink, which makes the onclick functionality inaccessible to keyboard users.
  • Michel Hébert
    Michel Hébert about 7 years
    I'd also add – from an accessibility perspective – that setting the href value to anything else than a relative/absolute path will cause screen reader issues. Avoid using a hash/pound symbol as workaround for this issue as it is not meant for this. Screen readers may (currently VoiceOver does) announce your link to be a fragment identifier (link to somewhere in the current page) and is also valid from a standards point-of-view. see w3.org/2001/tag/2011/01/HashInURI-20110115#NakedHash
  • EML
    EML about 4 years
    I'm finding that href="" is reloading the page in a new tab - no idea why. I have to explicitly add target="_self"
  • Robert Siemer
    Robert Siemer about 4 years
    This is illegal syntax. According to the spec “[Empty Attribute] syntax is permitted only for boolean attributes.”
  • doug65536
    doug65536 over 2 years
    Why take so much pity on screen readers though? They have if statements too.