Validating XHTML containing URLs that use ampersands
A very common method of passing data to a server is through the use of the query string. If you're not familar with what the query string is, you can read about about it at the Wikipedia article on query strings - all you need to know is that the query string can sometimes use the ampersand (&) character.Query strings are pretty harmless, but the problems start when we need to link to a page using the query string. Consider this example - you are link to a game demo download, and the url looks like this:
http://www.example.com/download.php?downloadarea=games&downloadid=12345
Normally we just put the URL straight into the anchor tag's href attribute. But if we did that using the URL we used above, we'd get a validation error. This is because the validator is expecting the & to start a new entity reference, which obviously it isn't.
How to stop ampersands causing validation errors
The solution is actually pretty simple. All you need to do is replace the & characters in the URL with the entity reference &. So if we were to have the link shown above in our HTML, it would look something like this:
<a href="http://www.example.com/download.php?downloadarea=games&downloadid=12345" title="game demo download">Click here to download</a>
Using this technique, your page will now validate, and browsers will simply swap out the entity reference for a real & character when the need to use the URL.
Back
12.03.2006.