473,654 Members | 3,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting string content to html

Hi all.

Well, I need some light in this simple thing I'm trying to do. I'm
using the XMLHttpRequest to retrieve some data from a db via php
script. The result is passed to a "results" array of strings, which
contain the data from the script.

But, the data from the script, which are the content of the strings,
have html tags in it. The thing is: I'm using the DOM to append the
string result to a table already built in the page (using
create_element and appendchild).

insertO = document.getEle mentById("outpu t_table");
oTR = document.create Element('tr');
oTD = document.create Element('td');
oText = document.create TextNode(Text);
oTD.appendChild (oText);
oTR.appendChild (oTD);
insertO.tBodies[0].appendChild(oT R);

The Text var would contain something like "<font color="red">thi s
<b>is</b> html</font>".

This way the output isn't parsed, i.e., it shows the tags.
Does anyone have an idea on how to overcome this?

Thks
Alex

Jul 23 '05 #1
6 1510
whoops, I guess I slightly misread your post. You'd still want to do a
regexp replace by using this:

/<[^<>]*>//ig as the expression.

Jul 23 '05 #2
You could just do a regexp replace for < and > for their HTML character
equivilents, &lt; and &gt; respectivly.

Jul 23 '05 #3
Alex wrote:
Hi all.

Well, I need some light in this simple thing I'm trying to do. I'm
using the XMLHttpRequest to retrieve some data from a db via php
script. The result is passed to a "results" array of strings, which
contain the data from the script.

But, the data from the script, which are the content of the strings,
have html tags in it. The thing is: I'm using the DOM to append the
string result to a table already built in the page (using
create_element and appendchild).

insertO = document.getEle mentById("outpu t_table");
oTR = document.create Element('tr');
oTD = document.create Element('td');
oText = document.create TextNode(Text);
oTD.appendChild (oText);
oTR.appendChild (oTD);
insertO.tBodies[0].appendChild(oT R);

The Text var would contain something like "<font color="red">thi s
<b>is</b> html</font>".

This way the output isn't parsed, i.e., it shows the tags.
Does anyone have an idea on how to overcome this?

Thks
Alex


Did you want the HTML to be parsed, or discarded? If it's the former,
this might be a textbook case for the value of innerHTML....

http://www.developer-x.com/content/innerhtml/

Jul 23 '05 #4
RobB wrote:
[...]

Did you want the HTML to be parsed, or discarded? If it's the former,
this might be a textbook case for the value of innerHTML....

http://www.developer-x.com/content/innerhtml/


Or a textbook case for ensuring that the text object(s) were
appropriately serialized in the XML in the first place. ;-)

(I make no admission to knowing what that format might be!)

Which requires the obvious response: supposing I wanted to use DOM to
create text nodes for the above case, what is the appropriate format
so that I can create appropriate DOM objects then give them
appropriate attributes?

Below is a feeble attempt to get a start on it, but I could not work
out how to read styles from an object then apply them to an element.

e.g. if I create a span 'oSpan' and put some text into it with a text
node, then create say w='color' and y='red', the following does
not seem to work:

oSpan = document.create Element('span') ;
oSpan.appendChi ld(document.cre ateTextNode('st uff'));
oSpan.style.w = y;

Then append oSpan to the document, 'stuff' is in the default
color. If I explicitly use:

oSpan.style.col or = 'red';
or
oSpan.style.col or = y;

'stuff' is red.

If there really is no way to do this? Do I need a case statement for
every single style attribute I want to support? If so, why isn't
innerHTML (or some similar method) available in DOM 3? Or is there
and I'm just displaying my ignorance?
<style type="text/css">
..rg {color:red;}
</style>
<script type="text/javascript">
function addStuff(z){
var tgt = document.getEle mentById(z);

// obj should be created by parsing XML, save that for later...
var obj = {};
obj[0] = {text:'this ','color':'red' }
obj[1] = {text:'is','fon tWeight':'bold' }
obj[2] = {text:' html','fontWeig ht':'normal'}

var i = 0;
var oTxt, oSpan;

do {
for (w in obj[i]){
switch (w) {

case 'text':
alert('Creating : ' + w + ' \'' + obj[i][w] + '\'');
oSpan = document.create Element('span') ;
oTxt = document.create TextNode(obj[i][w]);
oSpan.appendChi ld(oTxt);
break;

default:
alert('Setting style: ' + w + ' to \'' + obj[i][w] + '\'');
// oSpan.style.w = obj[i][w];
// oSpan.style.w = '\'' + obj[i][w] + '\'';
// oSpan.style.fon tWeight = 'bold';
oSpan.style.col or = obj[i][w];
}
}
alert('adding ' + oSpan.nodeName) ;
tgt.appendChild (oSpan);
}
while ( obj[++i] )
}
</script>
<input type="button" onclick="addStu ff('xx')" value="addStuff ">
<br>
<span id="xx"></span><br>
<span class="rg">samp le text</span>


--
Rob
Jul 23 '05 #5
RobG wrote:
<snip>
e.g. if I create a span 'oSpan' and put some text into
it with a text node, then create say w='color'
and y='red', the following does not seem to work:

oSpan = document.create Element('span') ;
oSpan.appendChi ld(document.cre ateTextNode('st uff'));
oSpan.style.w = y;

Then append oSpan to the document, 'stuff' is in the default
color.
That gives you a property of the style object called 'w' to which the
string value 'red' has been assigned. There is no reason to expect a 'w'
property of a style object to affect the presentation of the span.
If I explicitly use:

oSpan.style.col or = 'red';
or
oSpan.style.col or = y;

'stuff' is red.

If there really is no way to do this? ...

<snip>

oSpan.style[w] = y;

Richard.
Jul 23 '05 #6
Richard Cornford wrote:
RobG wrote:
<snip>
e.g. if I create a span 'oSpan' and put some text into
it with a text node, then create say w='color'
and y='red', the following does not seem to work:

oSpan = document.create Element('span') ;
oSpan.appendChi ld(document.cre ateTextNode('st uff'));
oSpan.style.w = y;
[...]
oSpan.style[w] = y;


Dang - thanks!

But I still find accessing properties this way a bit confusing.
--
Rob
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8667
by: Alfredo Agosti | last post by:
Hi folks, I have an Access 2000 db with a memo field. Into the memo field I put text with bold attributes, URL etc etc What I need to to is converting the rich text contained into the memo field in plain text. That's because I truncate the string text but if, when I truncate it there's a rich tag the code following the truncated string is corrupted. Don't know if it's clear enough.
1
3348
by: Bill Sneddon | last post by:
I am using an XML file produced by doing a save-as in Excel. The file has content that looks like one of these three examples lines: <Cell ss:StyleID="s24"><ss:Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40">H<Sub>3</Sub><Font>PO</Font><Sub>4 </Sub></ss:Data></Cell> <Cell ss:StyleID="s24"><Data ss:Type="String">GC</Data></Cell> <Cell ss:StyleID="s24"><ss:Data ss:Type="String"
1
1972
by: KK | last post by:
Hi, I need to save certain content to SQL server and then use SQL Server indexing facility to search. SQL Server have recomended using Image data type with a filter type (for .htm, .doc etc..) Now, because of this I save normal html content also in binary format in the Image field.
2
1670
by: Dave | last post by:
I am converting an ASP Classic web site to an ASP.NET application and have some real basic formatting or design questions. (The classic ASP site has both code (forms) and content pages.) 1.When moving a significant amount of content from the ASP classic page (basically formatted text in <P> tags) to an ASP.NET webform, is it better to add it to the webform as HTML or should I put each paragraph into separate (or perhaps a single) ...
20
5618
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just client-side HTML, CSS, etc. What I want to do is somehow insert a *server control* into the , then set the server control's properties at runtime.
5
2514
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant applications. I am testing an upgrade of all of the sites and have converted the main root site...although not necessarily fixed any issues. I move on instead and converted one of the virtual roots that is a seperate
4
1184
by: Phill | last post by:
I am trying to convert the following to VB: Imports System.Data Imports System.Data.SqlClient Imports System.Net Imports System.IO.Stream 'Get the stream containing content returned by the server. Dim dataStream = response.GetResponseStream()
4
1777
by: pmcgover | last post by:
I enjoyed Paul Barry's September article in Linux Journal entitled, "Web Reporting with MySQL, CSS and Perl". It provides a simple, elegant way to use HTML to display database content without any sql markup in the cgi script. The cgi script simply calls the Mysql command line with the HTML option (-H) and the SQL script file directed to that command. This provides complete separation of the markup from the sql code. The plain vanila...
0
1094
by: DougBatch | last post by:
I am storing resumes in SQL2005 so that I can make use of full text search. The resumes (word and txt docs) are stored as images. I would like to be able to highlight the keywords that are found in the resumes when the full text search is returned. I have managed to do this by returning the resume, putting the content into a byte array and then putting the content of the byte array into a string with enc.GetString(MyByteArray). I can then use...
0
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.