469,602 Members | 1,713 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Function to get HTML entities?

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
Jul 22 '08 #1
6 1787
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;
}

// 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/
Jul 22 '08 #2
On Jul 22, 10:42*am, Martin Honnen <mahotr...@yahoo.dewrote:
laredotornadowrote:
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;

}

// 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/
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
Jul 22 '08 #3
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 22 '08 #4
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) + ";";
});
}
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jul 23 '08 #5
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
Jul 23 '08 #6
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
Jul 23 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Frank Thorstens | last post: by
2 posts views Thread by micha | last post: by
6 posts views Thread by Horst Gutmann | last post: by
4 posts views Thread by Geoff Wilkins | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
6 posts views Thread by clintonG | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.