Posted By

wizard04 on 09/05/08


Tagged

escape javascript html encode amphersand


Versions (?)

Avoid "Helpful" Encoding of Amphersands


 / Published in: JavaScript
 

  1. //Sometimes, setting an element attribute (e.g., title) with javascript automatically escapes all amphersands in the
  2. // string, apparently to be "helpful". It also does this when creating a text node with document.createTextNode.
  3. //This function gets around this by setting innerHTML on a temporary element and returning the text node's value.
  4. function literalText(str)
  5. {
  6. var tmp = document.createElement("div");
  7. tmp.innerHTML = str;
  8. return (tmp.firstChild ? tmp.firstChild.nodeValue : "");
  9. }

Report this snippet  

You need to login to post a comment.