TIPS & TRICKS

JavaScript Hybrids (Extending the browser) – Part 1

I deeply enjoy browser programming, however sometimes I wish it could do more. Things like sockets, streams, audio and improved file system handling would be a real treat. Man would it be fresh if I had access to this functionality in JavaScript.

Now this is going to sound pretty circa 98, but several main stream browser plugins support a JavaScript communication layer. According to the Millward Brown survey plugin installations of Flash (99%) and Java (85%) are pretty ubiquitous.

Flash/JavaScript Communication
The Flash ExternalInterface class enables communication between JavaScript and the Flash Player. ExternalInterface was first introduced in ActionScript 1.0; so Flash Player 8 is the minimum plugin version required.
From JavaScript

  • Call an ActionScript function
  • Pass arguments
  • Return a value to the JavaScript callee

From ActionScript

  • Call a JavaScript function
  • Pass arguments
  • Pass various data types (Boolean, Number, String, etc…)

Java Applet/JavaScript Communication
The scarcely documented LiveConnect API provides JavaScript with the ability to call methods of Java classes and vice-versa. Using LiveConnect in applets requires the mayscript attribute and the plugin.jar package for newer versions of Java (Howto for Mac OS X users). Communication from Java to JavaScript is mitigated through the netscape.javascript.JSObject class. JavaScript exceptions in Java can be handled using the netscape.javascript.JSException class. Public methods in an applet can be called using the applet container object followed by the method name and arguments (e.g., document.getElementById(“myapplet”).publicAppletMethod(arg1, argN);).

From JavaScript

  • Call a Java method
  • Pass arguments
  • Return a value to the JavaScript callee

From Java

  • Call a JavaScript function (Note: does not seem to support deep objects obj.foo(arg))
  • Pass arguments
  • Pass various data types (Boolean, Number, String, etc…)

It looks like LiveConnect is due for an overhaul in the near future, so you may want to keep your eyes out for changes on Mozilla developer Josh Aas’s blog.

What’s Next
With the power of Java and Flash this opens up the arena for creating visually hidden gateways (i.e., width:0px; height:0px; applets or swf movies) that extend the browser. Stay tuned for the next part in this series where we make a sample application. Feel the power!

----------------------------------------------------
Thanks!
Carl Yestrau

Splunk
Posted by

Splunk