Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function to get HTML entities?

0 views
Skip to first unread message

laredotornado

unread,
Jul 22, 2008, 12:18:14 PM7/22/08
to
Hi,

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 "&lt;"?

Thanks, - Dave

Martin Honnen

unread,
Jul 22, 2008, 12:42:37 PM7/22/08
to
laredotornado wrote:

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;
}

// use like this

htmlEscape('a < b && b > c')

// result: a &lt; b &amp;&amp; b &gt; c


There are however lots of HTML entities defined in the HTML 4 DTD that
the above approach does not cover. So it depends on which characters
exactly you want to replace with entity references.


--

Martin Honnen
http://JavaScript.FAQTs.com/

laredotornado

unread,
Jul 22, 2008, 12:47:19 PM7/22/08
to
On Jul 22, 10:42 am, Martin Honnen <mahotr...@yahoo.de> wrote:
> laredotornadowrote:

Thanks for this excellent function, Martin. Just so the state of the
world is the same as when I entered it, how do I remove the div I
just created from the existing DOM after I get the HTML vals?

- Dave

Martin Honnen

unread,
Jul 22, 2008, 1:03:18 PM7/22/08
to
laredotornado wrote:

> Thanks for this excellent function, Martin. Just so the state of the
> world is the same as when I entered it, how do I remove the div I
> just created from the existing DOM after I get the HTML vals?

The div element object is created inside the function but never inserted
anywhere in the document so there is no need to remove it from the document.

Thomas 'PointedEars' Lahn

unread,
Jul 23, 2008, 1:53:49 PM7/23/08
to

The most efficient, reliable and easy to maintain way nowadays is probably

function htmlEncode(s)
{
return s.replace(
/[<>&]/g,
function(m) {
return "&" + m.charCodeAt(0) + ";";
});
}


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Thomas 'PointedEars' Lahn

unread,
Jul 23, 2008, 1:55:30 PM7/23/08
to
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 "&lt;"?
>
> 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

Thomas 'PointedEars' Lahn

unread,
Jul 23, 2008, 1:57:44 PM7/23/08
to
Thomas 'PointedEars' Lahn 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 "&lt;"?
>
> The most efficient, reliable and easy to maintain way nowadays is probably
>
> function htmlEncode(s)
> {
> return s.replace(
> /[<>&]/g,
> function(m) {
> return "&" + m.charCodeAt(0) + ";";
> });
> }

Supplemental: This does not meet the described outcome exactly, but it works
anyway (sometimes even better than character entity references) and can
easily be extended for other characters.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

0 new messages