Jquery - convert ascii characters to HTML

I am reading data from a html file to be loaded into a div. The problem I am facing is that the program that writes the html files converts <br />

to&lt;br /&gt;

SO when doing

$('#items').load('/News/list.aspx');

      

it displays <br />

as a string on my page rather than reading it as a page break.

I tried to read the above file into a variable to replace the string with &lt;br /&gt;

, but doesn't seem to work.

Any suggestions?

+2


a source to share


2 answers


The first step would be to modify the server side of the script if possible so that the HTML doesn't get encoded in the first place.

Otherwise, you can use the method ajax

to load the page data into a string first. The method you are using just downloads the text immediately.



$.ajax({
    type: 'GET',
    url: '/News/list.aspx',
    dataType: 'text',
    success: function(response) {
        response = response.replace( /&lt;/g, '<' );
        response = response.replace( /&gt;/g, '>' );
        $('#items').html(response);
    }
});

      

Here I have replaced the individual angle brackets that convert everything back to HTML. If you only want line breaks and nothing else, replace those two lines withresponse = response.replace( /&lt;br \/&gt;/g, '<br />' );

+3


a source


Is it url-encoded <and> tags?

Have you tried decoding the string you return with something like this?

http://plugins.jquery.com/project/URLEncode



NTN

EDIT: This is not url encoding, so it won't work.

0


a source







All Articles