Un esempio di come codificare e decodificare una stringa in html usando JavaScript puro.

Ci sono due funzioni sgart.htmlEncode(str) e sgart.htmlDecode(str):
var sgart = sgart || {};

sgart.htmlEncode = function (str) {
    if (!sgart.__htmlEncodeDecode_DIV) {
        sgart.__htmlEncodeDecode_DIV = document.createElement("div");
        sgart.__htmlEncodeDecode_DIV_textContent_enabled = "textContent" in sgart.__htmlEncodeDecode_DIV;
    }
    if (sgart.__htmlEncodeDecode_DIV_textContent_enabled) {
        sgart.__htmlEncodeDecode_DIV.textContent = str;
    } else {
        sgart.__htmlEncodeDecode_DIV.innerText = str;
    }
    return sgart.__htmlEncodeDecode_DIV.innerHTML;
};

sgart.htmlDecode = function (str) {
    if (!sgart.__htmlEncodeDecode_DIV) {
        sgart.__htmlEncodeDecode_DIV = document.createElement("div");
        sgart.__htmlEncodeDecode_DIV_textContent_enabled = "textContent" in sgart.__htmlEncodeDecode_DIV;
    }
    sgart.__htmlEncodeDecode_DIV.innerHTML = str;
    return sgart.__htmlEncodeDecode_DIV_textContent_enabled ? sgart.__htmlEncodeDecode_DIV.textContent : sgart.__htmlEncodeDecode_DIV.innerText;
};
che possono essere usate in questo modo:
sgart.htmlEncode("<div style=\"border:0\">");    // &lt;div style="border:0"&gt;

sgart.htmlDecode("&lt;div style=\"border:0\"&gt;");    // <div style="border:0">