Javascript HtmlEncode/HtmlDecode

Have you ever needed to HtmlEncode a querystring using JavaScript? Yes you may say, and several people will claim that you should UrlEncode it using escape(str); For some unknown reason I tried doing exactly that and UrlDecode the variable in csharp with no luck. Instead I found a solution in the Prototype library, appropriately they call it escapeHTML:

function escapeHTML (str)
{
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
};


Wonderful simple and effective trick using the browsers own automatic Html converter.

↓ Add Comment
↓ Add Comment

Comments (0)