Avoid "Helpful" Encoding of Amphersands

/ Published in: JavaScript
Save to your folder(s)

Copy this code and paste it in your HTML
  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


RSS Icon Subscribe to comments

You need to login to post a comment.