Return to Snippet

Revision: 8182
at September 12, 2008 16:14 by wizard04


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

Revision: 8181
at September 12, 2008 16:10 by wizard04


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

Revision: 8180
at September 5, 2008 13:48 by wizard04


Initial Code
//Sometimes, setting an element attribute with javascript automatically converts all instances of "&" in the string
// to "&", apparently to be "helpful".
//It also does this when creating a text node with document.createTextNode.
//This function gets around this by setting innerHTML on a temporary element and returning the text node's value.
function avoidHelpfulFormatting(str)
{
	var tmp = document.createElement("div");
	tmp.innerHTML = str;
	return (tmp.firstChild ? tmp.firstChild.nodeValue : "");
}

Initial URL


Initial Description


Initial Title
Avoid "Helpful" Encoding of Amphersands

Initial Tags
javascript, html

Initial Language
JavaScript