Bluish Coder

Programming Languages, Martials Arts and Computers. The Weblog of Chris Double.


E4X with Server Side Javascript

I've updated the Server Side Javascript distribution to include an example which uses E4X.

E4X is described in Ecma-357 and adds a Native XML syntax to Javascript. The XML is converted to a Javascript object which can be used to query and manipulate the XML.

The E4X example servlet generates the XHTML to be displayed using:

var result = 
        <head><title>HTML Using E4X!</title></head>
          <p>Hello from E4X on: {new Date()}.</p>

It embeds the current date and time by calling out to Javascript's Date object. This is done by wrapping the Javascript expression inside curly bracket: {new Date()}.

Given the XML object now stored in the 'result' variable it can be sent over the wire in the same manner as the other servlet examples:

var text = doctype + result.toString();

I prepend an XHTML doctype to the output as the XML generated is actually XML not HTML. This means things like <br> need to be <br/>, etc. I've not found a way to get E4X to generate HTML which is a pity - if anyone knows if it is possible I'd appreciate you dropping a comment here.

The updated code is available as javascript-server-0.2.tar.gz and in the darcs repository:

darcs get


This site is accessable over tor as hidden service mh7mkfvezts5j6yu.onion, or Freenet using key: