Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Twitter and Google API documentation encode ampersands in URLs?

I have read I should encode my ampersands as & in HTML.
However numerous code samples from respected companies somehow forget to do this.

Just a few examples off the top of my head:

Google Web Fonts sample code:

<link href='http://fonts.googleapis.com/css?family=PT+Sans&subset=latin,cyrillic' rel='stylesheet' type='text/css'>

Google Maps documentation:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&language=ja">

Twitter Anywhere official tutorial:

<script src="http://platform.twitter.com/anywhere.js?id=YOUR_API_KEY&v=1" type="text/javascript"></script>

Is there any real benefit from not escaping ampersand in links?
Is this related to browser quirks? Is this just a mistake in documentation?

Dear answerers, please make sure you're answering the right question.

I know I should escape ampersands per spec. I also know why the mechanism was invented in the first place. I'm not asking about this. My question is:

Is there a reason API documentation by respectable companies often violates this rule?

like image 599
Dan Abramov Avatar asked Jan 16 '12 00:01

Dan Abramov


People also ask

Can urls have ampersands?

For example, to encode a URL with an ampersand character, use %24. However, in HTML, use either &amp; or &#38;, both of which would write out the ampersand in the HTML page. These different encoding schemes aren't as contradictory as they seem.

How do you show ampersand in HTML?

In HTML, the ampersand character (“&”) declares the beginning of an entity reference (a special character). If you want one to appear in text on a web page you should use the encoded named entity “ &amp; ”—more technical mumbo-jumbo at w3c.org.

What is &amp in HTML?

& is HTML for "Start of a character reference". &amp; is the character reference for "An ampersand". &current; is not a standard character reference and so is an error (browsers may try to perform error recovery but you should not depend on this).


1 Answers

Two different contexts here.

  1. Within the context of a javascript href, the & is just fine and should not be encoded.
  2. In an HTML link the & is forbidden and should be escaped.

In the HTML link context an HTML character entity will be decoded before the address is passed to the HTTP process; a URL-encoded character will not, as the server can read it directly.

like image 110
Pandaski Avatar answered Nov 15 '22 20:11

Pandaski