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
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

83function htmlEncode(string) {
84 var HTML_CHARS = {
85 '&': '&',
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};
15static htmlEncode(string) {
16 return $('<div/>').text(string).html();
17}
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(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
109}
584function _htmlEncode(str) {
585 return str.replace(/[{}()\[\]<>!@#$%^&*\/\\'"+=,.?]/g, match => {
586 let charCode = match.charCodeAt(0);
587 return `#${charCode};`;
588 });
589}
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}
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}
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