"Escape character" HTML character entity < &GT: & etc.

Source: Internet
Author: User

The URL that JavaScript gets from the background in development is escaped, such as: http://localhost:8080/Home/Index?a=14&b=15&c=123, want to turn it into HTTP// Localhost:8080/home/index?a=14&b=15&c=123

Online search for a half-day solution:

Escaping is divided into escapehtml and unescapehtml, and the implementation of two functions is first seen.

JS Code:

1 /**2 * @function escapehtml Escape HTML Script < > & "'3 * @param A-4 * String5  */6Escapehtml:function(a) {7A = "" +A;8     returnA.replace (/&/g, "&amp;"). Replace (/</g, "&lt;"). Replace (/>/g, "&gt;"). Replace (/"/g," &quot; "). Replace (/'/g, ' &apos; ');;9 },Ten /** One * @function unescapehtml Restore HTML script < > & "' A * @param A- - * String -  */ theUnescapehtml:function(a) { -A = "" +A; -     returnA.replace (/&lt;/g, "<"). Replace (/&gt;/g, ">"). Replace (/&amp;/g, "&"). Replace (/&quot;/g, ' '). Replace (/&apos;/g, "'"); -},

1,escapehtml < > & "' into character entities
Usage scenarios:
(1) Users in the page input (such as input box) <script>alert (2); </script> JS will submit the content to the backend save
(2) When displayed, the backend returns the string to the front end; JS receives:
A, using escapehtml, convert the string to &lt;script&gt;alert (2);&lt;/script&gt; at this point, the browser will be able to parse correctly, because after the browser receives the entity characters, turn into the corresponding angle brackets, and so on.
b, do not use escapehtml, the browser to see <, it is considered the beginning of the HTML tag, directly put the string just now when the script executes, this is the XSS vulnerability.

2,unescapehtml Convert character entities to < > & "'
Usage scenarios:
The backend displays the escaped content to the page, such as &lt;script&gt;alert (2);&lt;/script&gt;
JS received after:
A, the front-end is unescapehtml, you can direct the DOM operation, the label will be displayed to the page.
b, the front end is not unescapehtml, the output is <script>alert (2); </script> but it is not executed at this time.

Escape characters:



Tip: The advantage of using entity names instead of numbers is that names are easy to remember. The downside is that the browser may not support all entity names (the support for entity numbers is good).

"Escape character" HTML character entity &lt; &GT: &amp; etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.