Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XHTML/HTML/JS syntax: When do I use &?

I am confused when I should use & and when I should use &, specifically when it's inside Javascript inside HTML.

Which (or both or neither) of these two URLs should use html-encoding?

<!-- Example 1 -->
<script type='text/javascript'>
    var myUrl = "myurl.html?bob=2&mary=3"; // or is it &amp;?
    downloadUrl(myUrl);
</script>

<!-- Example 2 -->
<p>Click <a href="myurl.html?bob=2&mary=3">here</a> <!-- &amp;? --> if the download doesn't start.</p>
like image 913
Scott Stafford Avatar asked Mar 09 '26 01:03

Scott Stafford


1 Answers

In HTML, you should use &amp; instead of &. In XHTML/XML, you must use &amp; instead of &. This also applies to inline JavaScript code. This doesn't apply to JavaScript code served by fullworthy .js files unless the text becomes part of the HTML DOM tree.

like image 146
BalusC Avatar answered Mar 11 '26 15:03

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!