10 examples of 'javascript html encode string' in JavaScript

Every line of 'javascript html encode string' 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
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}
83function htmlEncode(string) {
84 var HTML_CHARS = {
85 '&': '&',
86 '<': '<',
87 '>': '>',
88 '"': '"',
89 "'": ''',
90 '/': '/',
91 '`': '`'
92 };
93 return (string + '').replace(/[&<>"'\/`]/g, function (match) {
94 return HTML_CHARS[match];
95 });
96};
15static htmlEncode(string) {
16 return $('<div>').text(string).html();
17}</div>
83function htmlEncode(str) {
84 var div = document.createElement("div");
85 div.appendChild(document.createTextNode(str));
86 return div.innerHTML;
87}
1function htmlEncode(html) {
2 return document.createElement('a').appendChild(document.createTextNode(html)).parentNode.innerHTML;
3}
106function htmlencode(str) {
107 if (!str) { return ''; }
108 return str.replace(/&amp;/g, '&amp;').replace(//g, '&gt;');
109}
584function _htmlEncode(str) {
585 return str.replace(/[{}()\[\]&lt;&gt;!@#$%^&amp;*\/\\'"+=,.?]/g, match =&gt; {
586 let charCode = match.charCodeAt(0);
587 return `#${charCode};`;
588 });
589}
11function htmlEncode(html : string = '') : string {
12 return html.toString()
13 .replace(/&amp;/g, '&amp;')
14 .replace(//g, '&gt;')
15 .replace(/"/g, '"')
16 .replace(/'/g, ''')
17 .replace(/\//g, '/');
18}
58function htmlencode(html) {
59 return html
60 .replace(/&amp;/g, '&amp;')
61 .replace(//g, '&gt;')
62 .replace(/"/g, '"')
63 .replace(/'/g, ''')
64 .replace(/\//g, '/');
65}
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}

Related snippets