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, "&"). Replace (/</g, "<"). Replace (/>/g, ">"). Replace (/"/g," " "). Replace (/'/g, ' ' ');;9 },Ten /** One * @function unescapehtml Restore HTML script < > & "' A * @param A- - * String - */ theUnescapehtml:function(a) { -A = "" +A; - returnA.replace (/</g, "<"). Replace (/>/g, ">"). Replace (/&/g, "&"). Replace (/"/g, ' '). Replace (/'/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 <script>alert (2);</script> 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 <script>alert (2);</script>
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 < >: & etc.