AabinGunz
AabinGunz

Reputation: 12347

How to replace HTML encoded characters in JavaScript?

I have a XML which I parse and place in HTML, dynamically when asked for.

It has some line feed 
 which I want to replace with 
	, so it comes in proper readable format. For some reason 
 is not creating a LineFeed.

var lput=$(xml).find('lput_info').attr('output');

lput=lput.ReplaceAll("
", "
	");

'<table class="jpanelTable" >'+
    '<tbody>'+              
    '<tr width="100%">'+
        '<th class="thPanel">Output</th>'+
        '<td class="tdPanel"><pre class="preWrap">'+lput+'</pre></td>'+
    '</tr>'+                
    '</tbody>'+
'</table>'

Using this function from here:

String.prototype.ReplaceAll = function(stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while(index != -1){
        temp = temp.replace(stringToFind,stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

Update

.preWrap {
    overflow: auto;
    font-family: "Consolas",monospace;
    font-size: 9pt;
    text-align:left;
    background-color: #FCF7EC;
    overflow-x: auto; /* Use horizontal scroller if needed; for Firefox 2, not
    white-space: pre-wrap; /* css-3 */
    white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
    word-wrap: break-word; /* Internet Explorer 5.5+ */
    margin: 0px 0px 0px 0px;
    padding:5px 5px 3px 5px;
    white-space : normal; /* crucial for IE 6, maybe 7? */
}

Part of the xml input string:

"bpmz.a&#xa;/usr/lib/libdl.a(shr_64.o)&#xa;/usr/lib/libperfstat.a(shr_64.o)&#xa;/usr/lib/lib.a"

Upvotes: 0

Views: 880

Answers (1)

Cerbrus
Cerbrus

Reputation: 72857

Some people might suggest a (jQuery) function that creates a temporary div, places the text in there, then gets the content of the div, to decode the string.

Although the implementation of it is simple, it is rather unsafe, since you'd pretty much be asking for users to put all kinds of html tags in the string.

I'd suggest using these functions
(Requires a second function)

These are safer than the div alternative.

Upvotes: 1

Related Questions