Skip to content

Bringing the Browser To The Server (Just Discovered This)

John Resig posted an awesome article a little over a year ago, and I’m just getting to it. I’ve been developing a javascript presentation, primarily focusing on scope and functions, to a group of Java developers. I’ve used JSUnit and Firebug to work with javascript quite a bit, but frankly, I don’t really like either one of them that much. I wanted something to execute examples in the simplest way possible, and for me, as primarily a Java developer, that’s the command line with as little code as possible.

What Resig’s done, in a nutshell is port the native browser environment to Rhino. So you can take his environment script load it in Rhino, and run your examples. It’s perfect, and exactly what I was looking for.

Taking a look at Resig’s article, it’s trivial to get an environment set up. I used a standard project directory structure,

/
/lib/js.jar <-- Rhino jar
/src/env.js <-- Resig's env script
/src/prototype.js <-- Prototype
/src/assert.js <-- my assert script
/src/start.js <-- my start script
/tests/example1.html <-- an html page to work with

To run this, execute java -jar ./lib/js.jar, to start up the Rhino shell.

Next,

$ java -jar ./lib/js.jar
Rhino 1.7 release 1 2008 03 06
js> load('src/start.js');
js> write tests

Here’s what I put into the start script:

load('./src/env.js');
window.location='tests/example1.html';
load('./src/prototype.js');
load('./src/assert.js');

This loads the environment (env.js), then an html page for the DOM, then the prototype libraries, and finally an assertion script I wrote:

function assert(condition, affirmative, negative) {
        if(!condition) {
                return negative;
        }
        return affirmative;
}

And now I’m ready to execute my example scripts!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

One Trackback/Pingback

  1. [...] To start with, I used Rhino and env.js to create a more full featured javascript runtime environment in the jvm, as John Resig wrote about a while back. I wrote about this a while back. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*