The following javascript functions will be useful when working with special characters in HTML.
// This Fucntion will decode an entity to its original character
// eg & will become '&' and '"' will become "
function entityToText(entityText) {
var d = document.createElement("div");
d.innerHTML = entityText;
return d.firstChild.nodeValue;
}
// This Fucntion will encode a special character to its entity
// eg ‘&’ will become '&' and " will become '"'
function textToEntity(specialText) {
var d = document.createElement("div");
d.appendChild(document.createTextNode(specialText));
return d.innerHTML;
}
alert(textToEntity('<&>')); // returns <&>
No comments:
Post a Comment