10 examples of 'html encode javascript' in JavaScript

Every line of 'html encode javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
67function _htmlEncode(str) {
68 if (Array.isArray(str) || typeof str === 'object') {
69 return encodeURIComponent(JSON.stringify(str));
70 }
71 else {
72 return encodeURIComponent(str + '');
73 }
74}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

15static htmlEncode(string) {
16 return $('<div/>').text(string).html();
17}
1function htmlEncode(html) {
2 return document.createElement('a').appendChild(document.createTextNode(html)).parentNode.innerHTML;
3}
111function html_encode(html){
112 if(!html)return html;
113 var temp = document.createElement ("div");
114 (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html);
115 var output = temp.innerHTML;
116 temp = null;
117 return output;
118}
58function htmlencode(html) {
59 return html
60 .replace(/&/g, '&amp;')
61 .replace(/</g, '&lt;')
62 .replace(/>/g, '&gt;')
63 .replace(/"/g, '&quot;')
64 .replace(/'/g, '&#x27;')
65 .replace(/\//g, '&#x2F;');
66}
83function htmlEncode(string) {
84 var HTML_CHARS = {
85 '&': '&amp;',
86 '<': '&lt;',
87 '>': '&gt;',
88 '"': '&quot;',
89 "'": '&#x27;',
90 '/': '&#x2F;',
91 '`': '&#x60;'
92 };
93 return (string + '').replace(/[&<>"'\/`]/g, function (match) {
94 return HTML_CHARS[match];
95 });
96};
83function htmlEncode(str) {
84 var div = document.createElement("div");
85 div.appendChild(document.createTextNode(str));
86 return div.innerHTML;
87}
131function htmlEncode(value) {
132 // Code to prevent xss attack
133 var temp_div = document.createElement("div");
134 temp_div.textContent = value;
135 return temp_div.innerHTML;
136}
96function htmlencode(html) {
97 return html
98 .replace(/&/g, '&amp;')
99 .replace(/</g, '&lt;')
100 .replace(/>/g, '&gt;')
101 .replace(/"/g, '&quot;')
102 .replace(/'/g, '&#x27;')
103 .replace(/\//g, '&#x2F;');
104}
11function htmlEncode(html : string = '') : string {
12 return html.toString()
13 .replace(/&/g, '&amp;')
14 .replace(/</g, '&lt;')
15 .replace(/>/g, '&gt;')
16 .replace(/"/g, '&quot;')
17 .replace(/'/g, '&#39;')
18 .replace(/\//g, '&#x2F;');
19}

Related snippets