Javascript String htmlEncode()

Description

Javascript String htmlEncode()




String.prototype.htmlEncode = function () {
 return String(this)
  .replace(/&/g, '&')
  .replace(/"/g, '"')
  .replace(/'/g, ''')
  .replace(/</g, '&lt;')
  .replace(/>/g, '&gt;');

}

Javascript String HTMLEncode()

String.prototype.HTMLEncode = function() { 
 var temp = document.createElement ("div"); 
 (temp.textContent != null) ? (temp.textContent = this) : (temp.innerText = this); 
 var output = temp.innerHTML; 
 temp = null; /* ww  w  .  j a  v a  2  s  .c  om*/
 return output; 
}



PreviousNext

Related