Whats the difference between & and & in HTML5?

53,749

Solution 1

In HTML5, they are equivalent in that example. Traditionally, in HTML, only & was correct — but as with so many things, web developers blithely ignored this inconvenient rule and wrote bare ampersands everywhere. For their part, browsers just "did the right thing" and interpreted these ampersands as ampersands. HTML5 standardized this behavior, so now & is allowed by itself as long as what goes afterward does not look like an entity reference.

Solution 2

& is the html special character for &, which has a special meaning as the sigial for html special characters. The fact that href="&" works is a convenience granted by most browsers who are nice enough to properly deal with invalid HTML, but it is technically incorrect. You want to use & because the entity will be converted into & in the HTML code.

Consider if you used " instead of &. That would end the href value and make the HTML incorrect, right? You want to use ".

See: http://www.w3.org/html/wg/drafts/html/master/single-page.html#syntax-errors "Errors involving fragile syntax constructs" -- in fact it points out that using & is okay if it's not followed by a named character reference, but there are many of those; ideally you would be escaping attribute input more or less automatically.

Share:
53,749
Bhimbim
Author by

Bhimbim

Updated on April 03, 2020

Comments

  • Bhimbim
    Bhimbim about 4 years

    What is the difference between & and &?

    Like in the code bellow, are both working in the same way?

    <a href="mailto:EMAIL?subject=BLABLABLA&body=http://URL, SHORT DESCRIPTION"></a>
    <a href="mailto:EMAIL?subject=BLABLABLA&amp;body=http://URL, SHORT DESCRIPTION"></a>
    
  • Explosion Pills
    Explosion Pills about 11 years
    Can you point to the standardization of this behavior in the spec? I'm looking now...
  • Chuck
    Chuck about 11 years
    @ExplosionPills: It actually does it in kind of backwards way. Whereas traditional HTML specified & only as an entity marker, HTML5 specifically forbids only "ambiguous ampersands." It's in the "HTML Syntax" section of the spec.
  • Alohci
    Alohci about 11 years
    @ExplosionPills -The behaviour (as opposed to what is allowed or forbidden) is here: w3.org/html/wg/drafts/html/master/…
  • mamacdon
    mamacdon about 6 years
    "Ambiguous ampersand" is defined here: w3c.github.io/html/syntax.html#ambiguous-ampersand