Jump to content

need to translate html code in Javascript


legolas

Recommended Posts

From Javascript I do a responseXML-read from a php-script that sends some rawtext that includes the string <br />.It sends in the form of: <keyword><![CDATA[" . $text . "]]></keyword>When it displays on the screen, it is untranslated, that is the <br /> is written, but I want it translated into a line break instead.How do I accomplish this? Any suggestions would be welcomed!

Link to comment
Share on other sites

Using DOM I create av 'div' element, style it (absolute position, top, left, height and width) andthen I just append it with document.body.appendChild(mytext);and it displays-------------------------------Lorem ipsum dolor sit amet, consectetuer adipiscing elit.<br />Vestibulum pharetra pellentesque ligula.<br />Ut dui. -------------------------------but instead I want line breaks converted to read it as-------------------------------Lorem ipsum dolor sit amet, consectetuer adipiscing elit.Vestibulum pharetra pellentesque ligula.Ut dui. -------------------------------

Link to comment
Share on other sites

You have to append each child individually. That's how DOM works:

var A = new Array();A[0] = document.createTextNode("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");A[1] = document.createElement("br");A[2] = document.createTextNode("Vestibulum pharetra pellentesque ligula.");A[3] = document.createElement("br");A[4] = document.createTextNode("Ut dui.");var x;for (x=0;x<A.length;x++) {document.body.appendChild(A[x]);}

Link to comment
Share on other sites

To clarify, when you append a string to a node it creates a new text node and puts the string into the text node. Text nodes are literal, so if you assign "<br>" to a text node it writes that out as text, it does not get rendered as HTML. You can either create the individual <br> elements like Ingolme mentioned or you can use the innerHTML property to set the text instead of appendChild.

Link to comment
Share on other sites

I know, it's quite complicated. So the easy solution is to set it like this:document.body.innerHTML = A stringI don't really like it, because it's not standard, but sometimes there's no other option.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...