473,408 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Converting character entity codes into characters using JavaScript

Basically I'm looking for a function that will parse a string and
replace any entity codes it finds w/ the actual character.

For instance the function would convert "Hello World" into "Hello
World"

Has anyone ever heard of a function that can do that?

Thanks!

Dec 20 '05 #1
1 6758
Jimbo wrote:
Basically I'm looking for a function that will parse a string and
replace any entity codes it finds w/ the actual character.

For instance the function would convert "Hello World" into "Hello
World"


A HTML parser can do that so doing e.g.
var div = document.createElement('div');
div.innerHTML = "Hello World";
and then reading out the text content of the div element should do:

function htmlToText (htmlMarkup) {
var div = document.createElement('div');
div.innerHTML = htmlMarkup;
if (typeof div.innerText != 'undefined')
{
return div.innerText;
}
else if (typeof div.ownerDocument != 'undefined' &&
typeof div.ownerDocument.createRange != 'undefined')
{
var range = div.ownerDocument.createRange();
range.selectNodeContents(div);
return range.toString();
}
else if (typeof div.textContent != 'undefined')
{
return div.textContent;
}
}

var text = htmlToText("Hello World");
You might want to add some object detection to prevent errors in older
browsers like Netscape 4 or IE 4.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: David Komanek | last post by:
Hi all, I have a question if it is possible to manipulate the settings of character encoding in Ms Internet Explorer 5.0, 5.5 and 6.0. The problem is that the default instalation of Ms IE seems...
76
by: Zenobia | last post by:
How do I display character 151 (long hyphen) in XHTML (utf-8) ? Is there another character that will substitute? The W3C validation parser, http://validator.w3.org, tells me that this character...
19
by: Ian | last post by:
I'm using the following meta tag with my documents: <meta http-equiv="Content-Type" content= "text/html; charset=us-ascii" /> and yet using character entities like &rsquo; and &mdash; It...
1
by: User Axes Dean Eyed | last post by:
The following statement does not yield what I expect it to: var txtNode = document.createTextNode("ESI&minus;"); txtNode holds a value of "ESI&minus;" instead of "ESI-" where '-' is the...
2
by: Gidi | last post by:
Hi, I'm writing a C# win application program, and i need to transfer my hebrew letters from unicode to ascii, now if i use the ascii encoding it writes me ??? instead of the hebrew letter i've...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
11
by: Jean-François Michaud | last post by:
Hello all, I'm having a little problem, The UTF-8 parser we are using converts the newline entity ( ) within an attribute that we are using to paliate CSS limitations. After the parser has...
3
by: jake | last post by:
I am new to xml. I have a routine that parses xml files using a regular XmlReader class. Unfortunately, the XmlReader chokes (throws an exception) on character codes such as "&Eacute;". I...
7
by: tempest | last post by:
Hi all. This is a rather long posting but I have some questions concerning the usage of character entities in XML documents and PCI security compliance. The company I work for is using a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.