Martin Honnen wrote:
laredotornado wrote:
>Is there a Javascript way of taking a string of text and encoding it
such that its HTML entities are represented? For example, "<" would
be represented as "<"?
Well assuming you have script in the browser and HTML document you can
use an approach like this:
function htmlEscape (string) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(string));
return div.innerHTML;
}
This rather bloated, inefficient, and unreliable approach mixes proprietary
and standards-compliant features without previous runtime feature test. It
can be safely recommended against.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee