views:

85

answers:

4

I have a webservice that returns html string as response. I want to display the response in a div element. The response I am getting from service is

<span style="text-indent: 29px;padding-top:35px;font-size: 16pt;font-family: Arial;color: #FFFFFF;font-weight: bold;position: absolute;text-decoration: none;">89</span>
<span style="text-indent: 41px;padding-top: 58px;font-size: 8pt;letter-spacing: 1px;font-family: Arial;color: #E5DA95;font-weight: bold;position: absolute;text-decoration: none">89</span>
<span style="text-indent: 55px;padding-top: 58px;font-size: 8pt;letter-spacing: 1px;font-family: Arial;color: #E5DA95;font-weight: bold;position: absolute;text-decoration: none">-</span>
<span style="text-indent: 59px;padding-top: 58px;font-size: 8pt;letter-spacing: 1px;font-family: Arial;color: #E5DA95;font-weight: bold;position: absolute;text-decoration: none">92</span>
<img src="https://www.autocheck.com/members/img/freeLinkScore/scoreBanner4.gif" height="75" width="75" border="0">

I used the statement div.innerHTML = response;

The response is getting displayed as text instead of HTML. Can someone please tell me how to display the response as html inside the div.

+1  A: 

setting the innerHTML property of an element does not cause that content to be encoded; it's purpose is specifically for getting and setting the HTML inside an element.

If the response is being html encoded, double check the actual response text you're getting from the server and make sure it's not being encoded on the server side. The response you're seeing may simply be an HTML-rendered version of the text, which would explain how you're seeing triangle brackets instead of the actual html encoded text.

Nathan Ridley
Thanks Nathan,I found that the response is being encoded. Do we have any built in fuctions to decode the response and assign it to innerHTML of DIV element
kishore
Just don't encode it to start with. Or if it's happening by a process you don't have control over and you're calling it directly from your javascript, you'll need to create a function to search and replace all of the standard HTML encoded characters with their unencoded equivalents. I'd go with not encoding it to start with though.
Nathan Ridley
A: 

If you have control over that, have the web service send the response in a CDATA section "<![CDATA[" + response + "]]>".

melhosseiny
He wants it to be rendered as HTML, not as text. You've given the opposite answer to what he asked for.
Nathan Ridley
Ah yes, my bad. Edited.
melhosseiny
I tried using div.innerHTML = "<![CDATA[" +response + "]]>" but it did not render html
kishore
I meant that you should have the web service send the response in a CDATA section.
melhosseiny
A: 

Setting your div.innerHTML to something like

<span style="text-indent: 29px;padding-top:35px;font-size: 16pt;font-family: Arial;color: #FFFFFF;font-weight: bold;position: absolute;text-decoration: none;">89</span> 

isn't going to work because that text isn't html encoded, so javascript is treating the quotes like escape sequences. If it was html encoded you would see &quot; in place of the quotes. To encode it you should be able to use the Server.HtmlEncode method.

Ben
A: 

If that's your webservice, change its to output an un-HTML-encoded response.

Otherwise you will have to HTML decode the response before setting it as content. I looked for a lightweight decoder and couldn't find one that was more robust than simple regex.

So, this will probably work for your immediate needs, just beware that it can break on certain combinations of content (which seem unlikely in this case).

response = response.replace (/&lt;/ig, '<').replace (/&gt;/ig, '>');

.
PS: Be sure that this webserver can't supply you with malicious html, like <script> tags.

Brock Adams