ampersand in email address (href)
Im working with email address which has an ampersand in 开发者_StackOverflow中文版it, the user wants a 'contact us' link to open up a new message wit their address populated, I normally use href, but the ampersand is causing this not to work, any idea's?
here's what i have at the moment:
<a href="mailto:L&D@gha.org.uk? subject=MessageTitle&"
style="font-family: Verdana; font-size: large; font-weight: bold; color: #800000">#GHA Organisation Development</a>
Use &
instead of plain &
. Your href URL is inside HTML so it needs to be escaped properly to be valid HTML.
Actually, this is not an ambiguous ampersand, so it’s perfectly valid to use the HTML you suggested. The currently accepted answer is incorrect (even though I agree it’s a good idea to always encode ampersands to avoid confusion).
The HTML spec defines ambiguous ampersands as follows:
An ambiguous ampersand is a U+0026 AMPERSAND character (&) that is followed by one or more characters in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9), U+0061 LATIN SMALL LETTER A to U+007A LATIN SMALL LETTER Z, and U+0041 LATIN CAPITAL LETTER A to U+005A LATIN CAPITAL LETTER Z, followed by a U+003B SEMICOLON character (;), where these characters do not match any of the names given in the named character references section.
Entities that don’t end in a semicolon (like &
) are handled differently in attribute values, though:
If the character reference is being consumed as part of an attribute, and the last character matched is not a U+003B SEMICOLON character (;), and the next character is either a U+003D EQUALS SIGN character (=) or in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9), U+0041 LATIN CAPITAL LETTER A to U+005A LATIN CAPITAL LETTER Z, or U+0061 LATIN SMALL LETTER A to U+007A LATIN SMALL LETTER Z, then, for historical reasons, all the characters that were matched after the U+0026 AMPERSAND character (&) must be unconsumed, and nothing is returned.
But this doesn’t apply here since the next character is @
.
FWIW, I’ve researched this thorougly and wrote about my findings here: http://mathiasbynens.be/notes/ambiguous-ampersands
I’ve also created an online tool that you can use to check your markup for ambiguous ampersands or character references that don’t end with a semicolon, both of which are invalid. (No HTML validator currently does this correctly.)
I had the same error. & is automatically added when I sent the confirmation email link through the mail. It causes an error because I had sent mail in Plaintext format that is why & was automatically added. so, in that situation, I sent a send an email in HTML format. Then problem solved.
In my code I just added 'mail.IsBodyHtml = true'. for sending email in html format.
精彩评论