Tuesday, February 21, 2012

HTML coding mistakes: the "dreaded" ampersand (&) in URLs

coding HTML (or variants)? you probably know that, to put certain symbols, such as < , on a page, you need to write "&lt;", otherwise it'll be mistaken for a tag. for the ampersand ("and sign") itself, you'll need to write "&amp;"

interestingly enough, this is neglected when you write a URL, especially in an <a> tag. here's an example:

<a href="http://www.example.com/content?id=1234&view=normal">View Content</a>

notice the error? the ampersand needs to be replaced with "&amp;"

I suppose that, since it's within quotes, it doesn't have to be replaced with these character entities (the technical term for this type of replacement). well, that's not the case. for example, if you need to include a special character, such as ¤ in a URL, that would require something like "&#a4".