1//To do this simply create a element in the DOM tree and
2//set the innerText of the element to your string.
3//Then retrieve the innerHTML of the element.
4//The browser will return an HTML encoded string.
5
6function HtmlEncode(s)
7{
8 var el = document.createElement("div");
9 el.innerText = el.textContent = s;
10 s = el.innerHTML;
11 return s;
12}
13
14console.log(HtmlEncode('&;\'><"'));
15
16//expected output: &;'><"
1const decodeHTML = s => {
2 var str, temp= document.createElement('p');
3 temp.innerHTML= s;
4 str= temp.textContent || temp.innerText;
5 temp=null;
6 return str;
7}
8
9console.log(decodeHTML('<'));