unescape() and escape styles question

Discussion in 'Javascript' started by RK, Nov 12, 2008.

  1. RK

    RK Guest

    Perhaps I'm confused somwhere in my understanding, but I'm trying to
    deal with some escaped text from an XML file which includes html markup
    and it appears there are a couple of techniques for escape codes.

    (I hope I can describe my issue here without having to worry about how
    to escape it for display on this group!)

    On the server-side I extract some text from an XML file, which includes
    some style and content. The XML is simply:

    <text> &lt; b &gt;Title&lt; /b &gt; Moby Dick </text>

    This is ampersand escaped bold tags within the xml file. An XML parser
    on server side reads it nicely and sends it to my javascript on the
    client side.

    The javascript uses the XML content from an XmlHttpRequest and needs to
    stick it into a webpage with

    txtmsg = mystuff.getElementsByTagName("text")[0].firstChild.nodeValue;
    document.getElementById(thisline).innerHTML=txtmsg;

    and of course it shows up in my browser as

    <b>Title</b> Moby Dick

    ....instead of with the word 'title' in bold.

    I discovered a Javascript unescape() function, but it doesn't seem to
    work for me. It appears the companion function "escape()" in javascript
    uses percentage-signs to do the escaping.

    Is there some means to unescape the ampersand-based escapes in
    javascript, so that I can insert HTML that will get interpreted by the
    browser?


    Thanks in advance for any suggestions,

    Ross.
     
    RK, Nov 12, 2008
    #1
    1. Advertising

  2. On Nov 12, 5:23 pm, RK <ross@ross> wrote:
    [snip]
    > Is there some means to unescape the ampersand-based escapes in
    > javascript, so that I can insert HTML that will get interpreted by the
    > browser?

    Hi Ross,
    a little googling around made me find[1] this trick:

    function unescapeHTML(html) {
    var htmlNode = document.createElement("DIV");
    htmlNode.innerHTML = html;
    if(htmlNode.innerText)
    return htmlNode.innerText; // IE
    return htmlNode.textContent; // FF
    }

    [1] http://www.jibbering.com/faq/index.html

    Cheers

    Gabriel Gilini
     
    Gabriel Gilini, Nov 12, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SGFycnkgS2Vjaw==?=

    What is the .Net equivalent of jscript escape/unescape?

    =?Utf-8?B?SGFycnkgS2Vjaw==?=, Mar 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,872
    =?Utf-8?B?VGltIFN0YWxs?=
    Mar 9, 2005
  2. slomo
    Replies:
    5
    Views:
    1,633
    Duncan Booth
    Dec 2, 2007
  3. NetKev
    Replies:
    1
    Views:
    4,344
    Owen Jacobson
    Mar 4, 2008
  4. Tony Augustine
    Replies:
    0
    Views:
    389
    Tony Augustine
    Jul 22, 2010
  5. Philipp

    Escape/ Unescape HTML?

    Philipp, Dec 20, 2007, in forum: Javascript
    Replies:
    2
    Views:
    266
    Thomas 'PointedEars' Lahn
    Dec 21, 2007
Loading...

Share This Page