tags:

views:

351

answers:

2

I am reading data from an html file to load into a div. The problem I am having is, the program that writes the html files is converting <br /> to &lt;br /&gt;

SO when I execute

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

it displays <br /> as a string on my page instead of reading it as a page break.

I have tried to read the above file into a variable to do a string replace on the &lt;br /&gt; but it doesn't seem to work.

Any suggestions?

A: 

Is it URLencoding the < and > tags?

Have you tried decoding the string you get back with something like this? :

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

HTH

EDIT: Thats not URL encoding so this won't work.

DannyLane
url encoding would look something like this: %3Cbr%20%2F%3E
lunixbochs
Yup, I agree, but I had answered before the the examples were posted. will update my anser.
DannyLane
+1  A: 

First step would be modifying the server-side script if possible so that the HTML doesn't get encoded in the first place.

Failing that, you can use the ajax method to load a page's data into a string first. The method you're currently using just loads 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've replaced individual angle brackets, which will convert everything back to HTML. If you only wanted line breaks and nothing else, replace those two lines with response = response.replace( /&lt;br \/&gt;/g, '<br />' );

DisgruntledGoat
The program that creates the text file is a black box... and a pain in the butt in general. The ajax method you proposed only seems to work for the first set of < > it encounters... so it removes the first one in the text file but none of the others... crazy
sdmiller
@sdmiller: you're right, `replace` only replaces once when using strings. I've edited the answer to use a regex that should replace every occurrence.
DisgruntledGoat
Yeah.. it took me a few minutes but I figured it out! Thanks for you input.. saved me from the headache I was getting banging my head! ;)
sdmiller