What is &amp used for

FormsHtml

Forms Problem Overview


Is there any difference in behaviour of below URL.

I don't know why the & is inserted, does it make any difference ?

www.testurl.com/test?param1=test&current=true

versus

www.testurl.com/test?param1=test&current=true

Forms Solutions


Solution 1 - Forms

& is HTML for "Start of a character reference".

& is the character reference for "An ampersand".

&current; is not a standard character reference and so is an error (browsers may try to perform error recovery but you should not depend on this).

If you used a character reference for a real character (e.g. ™) then it (™) would appear in the URL instead of the string you wanted.

(Note that depending on the version of HTML you use, you may have to end a character reference with a ;, which is why &trade= will be treated as ™. HTML 4 allows it to be ommited if the next character is a non-word character (such as =) but some browsers (Hello Internet Explorer) have issues with this).

Solution 2 - Forms

HTML doesn't recognize the & but it will recognize & because it is equal to & in HTML

I looked over this post someone had made: http://www.webmasterworld.com/forum21/8851.htm

Solution 3 - Forms

My Source: http://htmlhelp.com/tools/validator/problems.html#amp

> Another common error occurs when including a URL which contains an > ampersand ("&"):

This is invalid:

> a href="foo.cgi?chapter=1§ion=2©=3&lang=en"

Explanation:

> This example generates an error for "unknown entity section" because > the "&" is assumed to begin an entity reference. Browsers often > recover safely from this kind of error, but real problems do occur in > some cases. In this example, many browsers correctly convert ©=3 > to ©=3, which may cause the link to fail. Since ⟨ is the HTML > entity for the left-pointing angle bracket, some browsers also convert > &lang=en to 〈=en. And one old browser even finds the entity §, > converting §ion=2 to §ion=2.

So the goal here is to avoid problems when you are trying to validate your website. So you should be replacing your ampersands with & when writing a URL in your markup.

> Note that replacing & with &amp; is only done when writing the URL in > HTML, where "&" is a special character (along with "<" and ">"). When > writing the same URL in a plain text email message or in the location > bar of your browser, you would use "&" and not "&amp;". With HTML, the > browser translates "&amp;" to "&" so the Web server would only see "&" > and not "&amp;" in the query string of the request.

Hope this helps : )

Solution 4 - Forms

That's a great example. When &current is parsed into a text node it is converted to ¤t. When parsed into an attribute value, it is parsed as &current.

If you want &current in a text node, you should write &amp;current in your markup.

The gory details are in the HTML5 parsing spec - Named Character Reference State

Solution 5 - Forms

if you're doing a string of characters. make:

let linkGoogle = 'https://www.google.com/maps/dir/?api=1'; 
let origin = '&origin=' + locations[0][1] + ',' + locations[0][2];


aNav.href = linkGoogle + origin;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionblue-skyView Question on Stackoverflow
Solution 1 - FormsQuentinView Answer on Stackoverflow
Solution 2 - FormsSamjusView Answer on Stackoverflow
Solution 3 - FormsJimView Answer on Stackoverflow
Solution 4 - FormsAlohciView Answer on Stackoverflow
Solution 5 - FormsUrbanek KrzysztofView Answer on Stackoverflow