this did not work.

G

greenflame

So I am trying to implement the following in DOM:


document.write(str);


I was wondering if the following would do it:

document.createT­extNode(str);

So I tryed it and it showed nothing.

I was wondering what will.
 
A

ASM

greenflame said:
So I am trying to implement the following in DOM:


document.write(str);


I was wondering if the following would do it:

document.createT­extNode(str);

So I tryed it and it showed nothing.

truc = document.creaTextNode(str);

create a node of text with 'str' as text and *nammed* 'truc'
*virtualy* on document
^^^^^^^^
but ...

not placed

so it is not displayed

JS wait you tell him where 'truc' has to stand

determine a 'place' to display it :
place = document.getElementById('somewhere')

insert 'truc' by its fellow 'place'
place.appenChild(truc);
or
place.parentNode.insertBefore(truc,place)

those childs, parents, (self = child of self-parent)
inserting (first, last, before), are not too easy to appropriate

Example :
=========

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Test</title>
</head><body>
<p id="paragraphe"></p>
<script type="text/javascript">
<!--
var horodator = document.createTextNode(document.lastModified);
var precedenttext = document.createTextNode("Last modified: ");
document.getElementById("paragraphe").appendChild(precedenttext);
document.getElementById("paragraphe").appendChild(horodator);
//-->
</script>
</body></html>

appendChild inserts 'truc' in end of container 'place'
 
R

RobG

greenflame said:
So I am trying to implement the following in DOM:


document.write(str);


I was wondering if the following would do it:

document.createT­extNode(str);

So I tryed it and it showed nothing.

I was wondering what will.

Having created your text node, you must add it to the document. Usually
you locate a suitable element using getElementById, or have a reference
passed to your write function from a calling function, then add your
text node to that.

So for your example:

<div id="textHolder"></div>
<input type="button" value="Add some text" onclick="
addText( 'Here is some text', 'textHolder' );
">

<script type="text/javascript">
function addText( str, elID ){
var aDiv = document.getElementById( elID );
var oTxt = document.createTextNode( str );
aDiv.appendChild( str );
}
</script>

Guessing at what you might use this for, here is a showMatrix() function
that will put a matrix (array of arrays) inside a table. You pass it a
reference to the matrix and the ID of the element to put the table
inside of (it should be a div or table cell or similar block level
element that can have an other block level element inside it).

The routine will handle a matrices with one or more rows of one or more
elements, but they must be matrices (or '2D arrays'). You can add
styles for classes 'mDiv' and 'mCell' to control the appearance of the
table.


// Show a matrix in a table
function showMatrix( X, elID ){

// Locate the element to add the matrix table to
var el = document.getElementById( elID );

// Check that X really is a matrix
if ( !X
|| 'object' != typeof X
|| Array != X.constructor ) {
var msg = 'showMatrix: didn\'t get a matrix';
el.appendChild( document.createTextNode( msg ) );
return;
}

// Setup variable
var i=0, j=X.length; // row counter
var m=0, n=X.length; // element counter
var cr; // current row

// Start making table & components
var oT = document.createElement('table');
var oTb = document.createElement('tbody');
var oTr, oTd;

// Create a new row
while ( i < j ) {
cr = X[i++];
oTr = document.createElement('tr');
m = 0;

// Append a cell for each element with the value inside
while ( m < n ) {
oTd = document.createElement('td');
oTd.className = 'mCell';
oTd.appendChild(document.createTextNode( cr[m++] ));
oTr.appendChild( oTd );
}

// Append the row to the table body
oTb.appendChild( oTr );
}

// Add a few niceties
oT.className = 'mTable';

// Add the body to the table and the table to the document
oT.appendChild( oTb );
el.appendChild( oT );
}
 
G

greenflame

Oh. See you gave me a url for a site that talks about displaying math
in webpages. From that I got this javascript that converts TeX to html
(with css, etc). So I just pass it the text in TeX format inside of a
span or div (depending on if I want inline or seperated math) with the
class set to math. So that is what I am using this for.

Also thanks for the help. I did not read that I need to attach the text
node to the document anywhere oh well....
 
R

RobG

greenflame said:
Oh. See you gave me a url for a site that talks about displaying math
in webpages. From that I got this javascript that converts TeX to html
(with css, etc). So I just pass it the text in TeX format inside of a
span or div (depending on if I want inline or seperated math) with the
class set to math. So that is what I am using this for.

Cool. Keep an eye on MathML, maybe one day it will become sufficiently
well supported that you can use it:

<URL:http://www.w3.org/Math/>

<URL:http://www.w3.org/Math/Software/>.


It hit version 1.0 in 1999 but you'd hardly know...

Poke around the links above, you'll find MathML editors, converters
(usually TeX/LaTeX to MathML) etc. Firefox/Mozilla provide native
support for MathML 1.0, IE requires a plugin.

[...]
 
G

greenflame

Ok... I tryed it but when I looked at the page it just showed the text
and it did not parse any of it as html. How do I get it do do this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top