By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

Function to get HTML entities?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.