Jquery - Convert ascii chars back to HTML
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 <br />
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 <br />
but it doesn't seem to work.
Any suggestions?
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( /</g, '<' );
response = response.replace( />/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( /<br \/>/g, '<br />' );
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.
精彩评论