Set a mailto link with a subject containing an ampersand (&) - html

Im using the following mailto link to send an email:
<a class="share3" title="" href="mailto:?subject=#check&body=#domain">
It works well, but sometimes my subject will contain an ampersand (&) character, and when it does my email is created without a body.
Any way to resolve this problem?

In order to get special/reserved characters into a URL, you must encode them - to get an & to work, it must be encoded to %26.
More details here: http://www.w3schools.com/tags/ref_urlencode.asp

use %26 for the & in the subject.

Related

HTML GET with & in a parameter

I fured out, that I got a problem with the "&" in a parameter, because the browser is interpreting it as a new parameter. Using urlencode doesn't help.
Example: http://www.example.com?artist=Brooks & Dunn&title=Maria
Is there a way to go around that problem?
Use URL encoding to encode unsafe characters in an URL.
http://www.example.com?artist=Brooks%20%26%20Dunn&title=Maria
See https://en.wikipedia.org/wiki/Percent-encoding

How to use two subsequent ASCII encoded characters in html links?

I want my e-mail link to set the e-mail subject to :
Some text & some other text
my tag is
mailto
and it sets the Subject only to
Some text
How do I correctly write the %20%26%20 (" & ") part?
Thanks
mailto:somemail#example.com?subject=some%20text%20%26%20some%20other%20text
I created an open-source tool for making this easy. Enter the strings you want and you'll instantly get the mailto:
mailto.now.sh
💌⚡️ Template full emails in a mailto

What other characters beside ampersand (&) should be encoded in HTML href/src attributes?

Is the ampersand the only character that should be encoded in an HTML attribute?
It's well known that this won't pass validation:
Because the ampersand should be &. Here's a direct link to the validation fail.
This guy lists a bunch of characters that should be encoded, but he's wrong. If you encode the first "/" in http:// the href won't work.
In ASP.NET, is there a helper method already built to handle this? Stuff like Server.UrlEncode and HtmlEncode obviously don't work - those are for different purposes.
I can build my own simple extension method (like .ToAttributeView()) which does a simple string replace.
Other than standard URI encoding of the values, & is the only character related to HTML entities that you have to worry about simply because this is the character that begins every HTML entity. Take for example the following URL:
http://query.com/?q=foo&lt=bar&gt=baz
Even though there aren't trailing semi-colons, since < is the entity for < and > is the entity for >, some old browsers would translate this URL to:
http://query.com/?q=foo<=bar>=baz
So you need to specify & as & to prevent this from occurring for links within an HTML parsed document.
The purpose of escaping characters is so that they won't be processed as arguments. So you actually don't want to encode the entire url, just the values you are passing via the querystring. For example:
http://example.com/?parameter1=<ENCODED VALUE>&parameter2=<ENCODED VALUE>
The url you showed is actually a perfectly valid url that will pass validation. However, the browser will interpret the & symbols as a break between parameters in the querystring. So your querystring:
?q=whatever&lang=en
Will actually be translated by the recipient as two parameters:
q = "whatever"
lang = "en"
For your url to work you just need to ensure that your values are being encoded:
?q=<ENCODED VALUE>&lang=<ENCODED VALUE>
Edit: The common problems page from the W3C you linked to is talking about edge cases when urls are rendered in html and the & is followed by text that could be interpreted as an entity reference (&copy for example). Here is a test in jsfiddle showing the url:
http://jsfiddle.net/YjPHA/1/
In Chrome and FireFox the links works correctly, but IE renders &copy as ©, breaking the link. I have to admit I've never had a problem with this in the wild (it would only affect those entity references which don't require a semicolon, which is a pretty small subset).
To ensure you're safe from this bug you can HTML encode any of your URLS you render to the page and you should be fine. If you're using ASP.NET the HttpUtility.HtmlEncode method should work just fine.
You do not need HTML escapement here:
According to the HTML5 spec:
http://www.w3.org/TR/html5/tokenization.html#character-reference-in-attribute-value-state
&lang= should be parsed as non-recognized character reference and value of the attribute should be used as it is: http://domain.com/search?q=whatever&lang=en
For the reference: added question to HTML5 WG: http://lists.w3.org/Archives/Public/public-html/2011Sep/0163.html
In HTML attribute values, if you want ", '&' and a non-breaking space as a result, you should (as an author who is clear about intent) have ", & and in the markup.
For " though, you don't have to use " if you use single quotes to encase your attribute values.
For HTML text nodes, in addition to the above, if you want < and > as a result, you should use < and >. (I'd even use these in attribute values too.)
For hfnames and hfvalues (and directory names in the path) for URIs, I'd used Javascript's encodeURIComponent() (on a utf-8 page when encoding for use on a utf-8 page).
If I understand the question correctly, I believe this is what you want.

Issue with ampersand in Input element

I have a form element on my ASP page, and under the <input> element there is a "value" attribute, in which I have an email recipient.
However, the email address has an ampersand (&) in it, and I'm not sure how to encode it for a HTML attribute...any help would be appreciated.
I currently have this format, but I am not receiving the emails:
<input type="hidden" name="recipient*" value="data&info#example.com">
Should I be using %26 to encode it, or is & correct? Or am I just doing it wrong?
Thanks for the help!
In HTML you should use &
%26 is used for URL encoding, when there's an ampersant in a parameter of the page, for example.
& should work fine, can you send/receive an email to that address manually?

w3c markup validator ampersand (&) error

Is there any workaround for the w3c validation error for an & present in urls or some other place in HTML markup?
It says:
& did not start a character reference. (& probably should have been escaped as &.)
The ampersand in my case is a part of a url for gravatar thumbnail. This is the problematic part of a url:
c91588793296e2?s=50&d=http%3A%2F%.
for each & sign you got write &
in your example it would be:
c91588793296e2?s=50&d=http%3A%2F%
Use & for literal ampersands, even in URLs.
http://htmlhelp.com/tools/validator/problems.html#amp
Replace with &
should be:
c91588793296e2?s=50&d=http%3A%2F%.
notice the &
I know it feels wonky, but ampersands have to be encoded as html entities, which are confusingly denoted with ampersands.