473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Inserting DOM nodes in-place at script elements in XHTML documents

SMH

I am in the process of converting all HTML documents, including many
dynamic/interactive documents, to XHTML documents (because I want to
incorporate SVG and MathML, among other things).

I am having a problem converting

document.write()

statements which are not allowed in XHTML documents to statements which
create the DOM nodes/elements dynamically within script elements.

Are there examples on the web or discussion in books about working with
XHTML?

I have a lot of books on Javascript and I think on DOM, but they don't seem
to talk about XHTML documents. I had to find out that document.write() was
not allowed from someone in a newsgroup, who directed me to the W3 FAQ on
this.
Jul 30 '08 #1
2 1940
SMH wrote:
I am in the process of converting all HTML documents, including many
dynamic/interactive documents, to XHTML documents (because I want to
incorporate SVG and MathML, among other things).

I am having a problem converting

document.write()

statements which are not allowed in XHTML documents to statements which
create the DOM nodes/elements dynamically within script elements.

Are there examples on the web or discussion in books about working with
XHTML?
As long as you serve your XHTML documents as text/html there is no
difference in scripting them compared to HTML documents.
And serving as application/xhtml+xml is not an option on the WWW in
general as long as IE does not render such documents.

On the other hand you say you want to use XHTML to incorporate SVG or
MathML so it sounds as if you indeed want to move to
application/xhtml+xml or application/xml. document.write does not work
but you can certainly use the W3C DOM Core methods to create and insert
nodes in your document. Assuming the browser does incremental parsing
and you want to insert nodes at the position where your script element
is in the document then you can use the following approach:
<script type="text/javascript">
var ns = 'http://www.w3.org/1999/xhtml';
var scripts = document.getElementsByTagNameNS(ns, 'script');
var lastScript = scripts[scripts.length - 1];

// now create content you want to insert e.g.
var p = document.createElementNS(ns, 'p');
p.appendChild(document.createTextNode('The quick ...'));
// and insert after the last/current script element
lastScript.parentNode.appendChild(p);
</script>
That should work with Firefox 3.0/2.0 and with Opera 9.
I don't think it works with Firefox 1 or 1.5. I have not tried with Safari.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 31 '08 #2
SMH
Martin Honnen <ma*******@yahoo.dewrote in comp.lang.javascript:
SMH wrote:
>I am in the process of converting all HTML documents, including many
dynamic/interactive documents, to XHTML documents (because I want to
incorporate SVG and MathML, among other things).

I am having a problem converting

document.write()

statements which are not allowed in XHTML documents to statements
which create the DOM nodes/elements dynamically within script
elements.

Are there examples on the web or discussion in books about working
with XHTML?

As long as you serve your XHTML documents as text/html there is no
difference in scripting them compared to HTML documents.
And serving as application/xhtml+xml is not an option on the WWW in
general as long as IE does not render such documents.

On the other hand you say you want to use XHTML to incorporate SVG or
MathML so it sounds as if you indeed want to move to
application/xhtml+xml or application/xml. document.write does not work
but you can certainly use the W3C DOM Core methods to create and
insert nodes in your document. Assuming the browser does incremental
parsing and you want to insert nodes at the position where your script
element is in the document then you can use the following approach:
<script type="text/javascript">
var ns = 'http://www.w3.org/1999/xhtml';
var scripts = document.getElementsByTagNameNS(ns, 'script');
var lastScript = scripts[scripts.length - 1];

// now create content you want to insert e.g.
var p = document.createElementNS(ns, 'p');
p.appendChild(document.createTextNode('The quick ...'));
// and insert after the last/current script element
lastScript.parentNode.appendChild(p);
</script>
Thanks Martin.

This should be put into a book with a lot of the many gold nuggets you
have posted in this newsgroup.

I hope you develop a relationship with a publisher.
That should work with Firefox 3.0/2.0 and with Opera 9.
I don't think it works with Firefox 1 or 1.5. I have not tried with
Safari.


Aug 1 '08 #3

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

Similar topics

4
by: abhrajit | last post by:
I'm looking for a C/C++/Java library to create a balanced binary tree data structure given a set of leaf nodes as input. A leaf node should never become an interior node. So if I wish to create...
2
by: ThunderMusic | last post by:
Hi, while I'm searching into a treeview to find a specific node, I find the code duplicating treeview nodes I just can't figure out what's causing this behavior, maybe some of you already...
2
by: ledder77 | last post by:
Hi, I need to find a way to insert a processing instruction to an xml document after all the other nodes have been correctly added. That's because I get an xml from a method that I can't modify...
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
6
by: DWrek | last post by:
Here is my problem. I have an XML document that is returned to me by a third party service. The XML document contains results for a search but only lists a maximum of 10 results. If there are any...
5
by: eva.mukhija | last post by:
Hi I need to insert some html content generated by certain business logic into an xsl output. I have been able to insert custom Java extensions and generate the HTML to be outputted. My problem...
1
by: GR | last post by:
Hi, I have something like the following: (Please note this not actual code and it's very rough) string source = "<abc:Root xmlns:abc="http://mynamespace.com><abc:Record/></abc:Root>"...
2
by: David Jackson | last post by:
Hello, I have a TreeView control with "top-level" nodes and "second-level" nodes (probably not the correct terms) as follows: objNode = new TreeNode(); objNode.Text = "2006";...
0
by: empiresolutions | last post by:
Howdy, I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set...
1
by: empiresolutions | last post by:
Howdy, I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.