views:

208

answers:

4

Hi, I have a reacurring problem. I code nice standards compliant code only to have it fail due to ampersands within some of the hyperlink urls.

Does anyone know of a work around or hack for this.

Thanks

+6  A: 

Did you make them & in the links like this?

&
Kevin
+10  A: 

I code nice standards compliant code only to have it fail due to ampersands within some of the hyperlink urls.

Unescaped ampersands in URLs (or anywhere else, if they're not part of an HTML entity!) aren't "nice standards compliant code".

Turn them into & and you can accurately claim to have done this.

ceejayoz
Grammar Nazi: /replace aren't with isn't
Henri Watson
Pekka
Thanks for the responses guys. URL encoding is a new one on me, I'm looking into it. There's a good link to the W3C URL encoding page provided by Ronald above.
Mr.K
+3  A: 

You should URL Encode the hyperlinks, so all characters are turned into a valid ASCII format and don't contain any (X)HTML entities.

For C# use HttpUtility.UrlEncode, for PHP urlencode, for JavaScript encodeURI, etc... Finding the right method for the language you're using shouldn't be that hard.

Ronald
`htmlspecialchars` (http://php.net/manual/en/function.htmlspecialchars.php) is actually better than `urlencode` for this.
ceejayoz
...I'd go as far as to call it *necessary*. `urlencode()` is the wrong approach. An ampersand has a special meaning in URLs (GET parameter separator) and `urlencode()` would *remove* that meaning by escaping them. We don't want to encode the ampersands in a URL context. We want to encode the ampersands in an HTML context. `htmlspecialchars()` does that.
pinkgothic
A: 

CDATA works wonders where you have & in javascript strings..

Nilesh