Languages

JavaScript - what special characters should be escaped in <a href=""> attribute?

8 points
Asked by:
starcraf35
754

I try to write own HTML escape function in JavaScript, but I am wondering if all special characters like <, >, &, " and ' should be escaped in <a href=""> attribute.

Anyone could confirm, my doubts?

2 answers
4 points
Answered by:
Root-ssh
178290

Possible solutions:

  1. You can find here information about universal HTML special characters escape function, so you can use it always with <a href=""> attribute.
  2. Alternatively, you can escape only selected characters depending on used HTML syntax:
    • ' character only for <a href='...'> syntax (' escaped to &#39;),
    • " character only for <a href="..."> syntax (" escaped to &quot; or &#39;),
    but more safety is to use Solution 1.

Note: do not forget to escape special characters in URI components using encodeURIComponent() function, what was described in this article.

0 commentsAdd comment
3 points
Answered by:
cory
1756

If you would like to escape only ' and " characters, it should be enough.

e.g.

 

0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join