JSXCompressor – zlib compressed JavaScript code

The open source library JSXGraph (http://jsxgraph.org) contains utilities to read files which
have been compressed by the ZLIB (http://zlib.org) library. That means, JSXGraph has a pure
JavaScript implementation of deflate, unzip and base64_decode. This can be used for delivering compressed JavaScript inside of an HTML file. Of course, with todays browsers it depends on the transmission bandwidth if this is worthwile. If the web server does not support compression of data, then this tool may be an option.

One possibility to compress the JavaScript source on server side is to use PHP. The code below writes the content of a JavaScript file as a compressed, base64 encoded string on-the-fly into the HTML. Then on client side this string can be accessed via the JavaScript variable jsxcompressed.

<?php
function jxgcompress($filename)
{
    if (file_exists($filename)) {
        $base64 = base64_encode(gzcompress(rawurlencode(file_get_contents($filename)),9));
        echo "var jxgcompressed = "$base64";\n";
    } else {
        throw new Exception("$filename not found");
    }
}
?>

<script type="text/javascript">
<?php
    jxgcompress("./helloworld.js");
?>
</script>

To uncompress and run this code on client side, the following code has to be included in the HTML code:

<script src="./jsxcompressor.js" type="text/javascript"></script>
<script type="text/javascript">
eval(JXG.decompress(jxgcompressed));
</script>

Thats all! The compression rate should be fairly good, since the compression algorithm is essentially gzip. There is some additional overhead since the compressed output has additionally to be base64 encoded.

Of course, this effort is useless, if the web server delivers the content already compressed with gzip.

The zip file jsxcompressor.zip contains two examples: testhelloworld.php and testjsxgraph.php. The jsxcompressor code is open source and can be downloaded here. The source code of the minified JavaScript file jsxcompressor.js consists of the file Util.js from the JSXGraph project, prepended by the string

JXG = {};
JXG.decompress = function(str) {
    return unescape(
                (new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(str))).unzip()[0][0]
             );
};

The source code of the JSXGraph library can be downloaded from http://sourceforge.net/projects/jsxgraph/.
JSXCompressor is released under the LGPL.

Enjoy,
Alfred Wassermann

This entry was posted in JSXCompressor and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted October 5, 2009 at 11:29 am | Permalink

    Another method to transfer compressed files without using mod_gzip or mod_deflate in apache is described in http://blog.avirtualhome.com/2009/09/29/using-gzip-encoding-in-apache-without-mod_gzip-or-mod_deflate/

  2. Posted October 22, 2009 at 1:16 pm | Permalink

    Today, Yahoo released its own client side compression library: http://browserplus.yahoo.com/demos/squeezr/intro

  3. Posted November 6, 2009 at 4:44 am | Permalink

    I have often read about this interesting format! I have long wanted to use it instead of raster graphics. In my opinion an excellent option for the site.

    It often for the design of the site do not necessarily use the full-color pictures, which is not bad and weigh. But this option could create a completely different design elements, and weigh much less, Another undeniable plus is that for them, and CSS can create. As a result, you can modify the elements themselves, and not to change the picture.

    So I would like to know more detail about it. How long is now feasible. Which browsers already supported as a simple files and animation. Just read that Maykrosoftovskom browser with the problem. A fox and Opera is normal work, and about Safari Googley browser does not hear.

    Thank you!
    ———————————————————————
    arcade flash shockwave java fun.

2 Trackbacks

  1. By JSXGraph Wordpress Plugin Update at webCONSUL on September 30, 2009 at 9:11 am

    [...] müsst ihr euch unbedingt noch das Release von JSXCompressor anschauen um komprimierten JavaScript-Code zu erzeugen und auszuliefern. Gerade bei Webservern die [...]

  2. [...] [...]

Post a Comment

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

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>