LoadForCompile: Help for in-browser compilers

August 13th, 2009

As javascript engines increase in speed and efficiency, it is becoming feasible to write compilers and interpreters in javascript to allow webpages to run alternative programming languages not normally supported by browsers. See processing.js, for instance: A graphics library that runs a Java-like language in the browser.

An initial hurdle facing developers of these toolkits is the problem of loading source code. Javascript is loaded and executed natively in the browser environment, and it would be nice to load alternative language sources in a similar manner. So, today I wrote a library to do that.

The library, called LoadForCompile, is invoked with a MIME type and a callback. It searches the DOM for script elements with the given MIME type, loads their sources, and passes them to the callback. There is a very simple demo page with a "compiler" that displays whatever source code it is provided. Following is the documentation for the library.

Description

LoadForCompile will retrieve alternate-language source code and pass it (in order) to onLoad. Any sources that cannot be retrieved will be ignored by default.

If the onFinish argument is provided, it will be called when all sources have been loaded and passed.

If the onError argument is provided, it will be called with (src, k), where src is the offending URL and k is a continuation of the loading process, so the relying client may continue or entirely halt the loading process by calling k or not calling it, respectively.

Usage

  1. Place script elements in your HTML document with desired MIME type.
  2. Each script element may have a src attribute, an inline body between the opening and closing tags, or both. The body is guaranteed to be called immediately after the associated remote source, or not at all.
  3. Finally, call LoadForCompile with the MIME type and callbacks.

Specification

LoadForCompile searches the DOM for any script nodes bearing the target MIME type. For each one, it records the src attribute and the contents of the text node, if any. It is an error to have non-text elements as children of the script node, and in such a situation the behavior of this library is unspecified.

The library uses asynchronous requests to load any remote resources specified by src attributes. This loading occurs in an unspecified order. If any remote source fails to load, the library will mark that source (and any associated inline body) as invalid. If onError is provided, it will be called with a continuation function that will resume progress. (If the continuation is not called, no additional callbacks will be made.)

Note that if onError is provided and does not call the continuation, onFinish will not be called.

Metadata

The library is in version 0.2 and is licensed under the LGPL.

Running Server-Side Javascript (SSJS) on Ubuntu

October 25th, 2008

Using JSEXT, I can now run javascript as a server-side scripting language on my Ubuntu box!

The main awesome thing about JSEXT is that a javascript file can be run either client- or server-side. This means that much your data validation code and some of your business logic can be kept in the same language, instead of splitting it into (for example) PHP + javascript. Furthermore, there's only one file to maintain for any given bit of logic.

Here's a quickstart guide to get JSEXT up and running with Apache 2 on Ubuntu 8.04 Hardy Heron:

Read full entry »

No-hassle IE movie embedding

October 5th, 2007

Due to a lawsuit by Eolas against Microsoft, Internet Explorer (both 6 and 7) is not allowed to auto-load movies, flash, and other plugins. The effect is that users must click an empty area with a gray outline to load the content or start interacting with it. The standard approach to route around this usability fiasco is to dynamically load the <object> and <embed> tags with javascript. Here I present an easier, more readable, and unobtrusive technique for embedding content in IE6 and IE7.

Read full entry »

Creating images with javascript

August 24th, 2007

If you feel like being a little bit silly today, you might check out some code I wrote to generate BMP images right in the browser, using javascript. For maximum compatibility, I'm using the v3 header and uncompressed 24-bit color.

All mine does so far is generate a quasi-random bitmap. The same technique could be used to generate very small rounded-corner images and other progressive enhancements. Perhaps some other file formats might be interesting to play with as well. Excel spreadsheets, perhaps? I don't have much interest in continuing work on javascript-based file production, but I'd love to see other people take this idea for a spin.

Unobtrusive Permalinks

March 22nd, 2007

One of the problems with AJAX-style hash-based permalinks (as in use at WikiMapia) is that the browser's history becomes cluttered, and the user has to hit Back a large number of times to escape the page. By using location.replace, I've devised a way to keep the history pristine yet allow DOM-state permalinks.

Check out the demo. (Should be working now, thanks wioota.)