DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   JavaScript / DHTML / AJAX (http://www.daniweb.com/forums/forum117.html)
-   -   html entity decode (http://www.daniweb.com/forums/thread137235.html)

R0bb0b Jul 28th, 2008 3:46 pm
html entity decode
 
anybody have a browser compatible html entity decoding function?

R0bb0b Jul 28th, 2008 5:48 pm
Re: html entity decode
 
Found this online and it seems like they all use this concept.
function html_entity_decode(str)
{
    //jd-tech.net
    var  tarea=document.createElement('textarea');
    tarea.innerHTML = str; return tarea.value;
    tarea.parentNode.removeChild(tarea);
}

apparently the innerHTML is giving me a runtime error on ie, i can only use value. But value doesn't decode the html entities.

R0bb0b Jul 28th, 2008 8:28 pm
Re: html entity decode
 
Found a solution:
function html_entity_decode(str)
{
    try
        {
                var  tarea=document.createElement('textarea');
                tarea.innerHTML = str; return tarea.value;
                tarea.parentNode.removeChild(tarea);
        }
        catch(e)
        {
                //for IE add <div id="htmlconverter" style="display:none;"></div> to the page
                document.getElementById("htmlconverter").innerHTML = '<textarea id="innerConverter">' + str + '</textarea>';
                var content = document.getElementById("innerConverter").value;
                document.getElementById("htmlconverter").innerHTML = "";
                return content;
        }
}

this way you can still add the innerHTML, but to a div instead, and it also adds a textarea with it so it can be retrieved by the value of the textarea. then it clears out the div and leaves it blank.
Cat skinned.


All times are GMT -4. The time now is 6:38 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC