469,328 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Is there a deHTMLize function for strings?

Code:

a = document.createTextNode("< somestring >");

....will transform < into &lt; and > into &gt;

When I want to read that node later, I can only get it back in
this form: "&lt; somestring &gt;"

Is there a function that transforms such strings containing
HTML codes back to normal strings? Something like:

string deHTMLize (string);

What I need is to quote and send a part of the document when
user clicks a button, so I need the string without codes.
Apr 19 '06 #1
5 2180

"test" <te**@test.test> wrote in message
news:10******************************@40tude.net.. .
Code:

a = document.createTextNode("< somestring >");

...will transform < into &lt; and > into &gt;


I think what you want is encode() and decode()...

a = encode("< somestring >");

....and...

a = decode("&lt;%20somestring%20&gt;");

Apr 19 '06 #2
On Wed, 19 Apr 2006 10:43:51 GMT, Noozer wrote:
"test" <te**@test.test> wrote in message
news:10******************************@40tude.net.. .
Code:

a = document.createTextNode("< somestring >");

...will transform < into &lt; and > into &gt;


I think what you want is encode() and decode()...

a = encode("< somestring >");

...and...

a = decode("&lt;%20somestring%20&gt;");


But, there are no such functions in JavaScript? I can't find
them, and I get errors in javascript:

"encode is not defined"
Apr 19 '06 #3
test <te**@test.test> writes:
On Wed, 19 Apr 2006 10:43:51 GMT, Noozer wrote:
"test" <te**@test.test> wrote in message
news:10******************************@40tude.net.. .
Code:

a = document.createTextNode("< somestring >");

...will transform < into &lt; and > into &gt;


I think what you want is encode() and decode()...

a = encode("< somestring >");

...and...

a = decode("&lt;%20somestring%20&gt;");


But, there are no such functions in JavaScript? I can't find
them, and I get errors in javascript:

"encode is not defined"


I don't know them either.

But you can make a decoder relatively easily:
---
var conversionMap = {
"amp" : "&",
"lt" : "<",
"gt" : ">",
"apos" : "'",
"quot" : '"'
}

function decode(entityString) {
return entityString.replace(/&(\w+);/g, function(m,g) {
return conversionMap[g]||m;
});
}
---
It requires a modern browser that accepts a function as second a
argument to replace.

If your string contains URL-escapes (%xx), you can first decode
these using the global "unescape" function (or after, depending
on what you want to unescape first).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 19 '06 #4

"test" <te**@test.test> wrote in message
news:1a******************************@40tude.net.. .
On Wed, 19 Apr 2006 10:43:51 GMT, Noozer wrote:
"test" <te**@test.test> wrote in message
news:10******************************@40tude.net.. .
Code:

a = document.createTextNode("< somestring >");

...will transform < into &lt; and > into &gt;


I think what you want is encode() and decode()...

a = encode("< somestring >");

...and...

a = decode("&lt;%20somestring%20&gt;");


But, there are no such functions in JavaScript? I can't find
them, and I get errors in javascript:


Doh... Sorry. It was too early.

I meant escape() and unescape() ...
Apr 19 '06 #5
On Wed, 19 Apr 2006 18:36:23 +0200, Lasse Reichstein Nielsen
wrote:
But you can make a decoder relatively easily:
---
var conversionMap = {
"amp" : "&",
"lt" : "<",
"gt" : ">",
"apos" : "'",
"quot" : '"'
}

function decode(entityString) {
return entityString.replace(/&(\w+);/g, function(m,g) {
return conversionMap[g]||m;
});
}
---
It requires a modern browser that accepts a function as second a
argument to replace.


Thanks much, works like a charm on Firefox 1.5.0.1.
Apr 20 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by bettina | last post: by
5 posts views Thread by pembed2003 | last post: by
35 posts views Thread by michael.casey | last post: by
5 posts views Thread by BBands | last post: by
17 posts views Thread by kleary00 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.