Web DevCenter
oreilly.comSafari Books Online.Conferences.
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA

Sponsored Developer Resources

Web Columns
Adobe GoLive
Essential JavaScript

Web Topics
All Articles
Scripting Languages

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab

O'Reilly Network Script Library -- Javascript

Simple Parser

Language: JavaScript

Download the files for this script (zip format)

This small application serves to purposes, it's an example of a very simple XML/XHTML parser as well as a required addition to DOM Level 1, which has no method defined to transform and insert plain markup into a document. The only method available, document.writeln(), can't be used to write into an existing document without destroying it.

Instead choosing one of proprietary methods available, this function only builds on top of standard DOM methods and transforms any valid plain markup into a corresponding DOM tree and inserts it into under a given element.


Supported platforms: You can use the writeCode() function with Mozilla / Netscape 6 and Internet Explorer 5.x.

The ZIP file contains two files:

  • write.js, JavaScript source file
  • index.html, an example page. Enter any markup and insert into your document with a click.

To insert this function into your own documents you have to import the JavaScript source file into your document:

<script src="write.js" type="text/javascript"></script>

Example call:

writeCode("<b>parse me</b>", document.getElementById('pointer'))

Calling this snippet will result in having the parsed output appended under the element with ID pointer.

To include your own markup, you have to assign an ID to an existing element by saying:

<div id="adBanner">..</div>

Now you can add new contents to this element by calling writeCode(markup, element_id)