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: &;'><"