Browsers

JavaScript Support in Java 6 Update 10

Tagged:  

The minor release with major features, Java 6 Update 10 brought a host of improvements to the long-neglected Java applet. One of these improvements was better interoperability with JavaScript, which was delivered to the new Java plug-in in the form of LiveConnect technology. As Sun's entrant into the RIA race, JavaFX, comes closer to a 1.0 release, such interoperability is crucial to the success of this effort.

While it has always been possible (to a certain extent) to call between an applet and JavaScript running the same browser, LiveConnect vastly improves the technology by

  • Improving reliability. A number of bugs involving the stability of the browser when making JavaScript<-->Java calls have been fixed.
  • Improving performance. Dramatically better performance...always a good thing.
  • Making it more consistent across different browsers. Support for JavaScript interoperability was always a little better on Mozilla browsers, but now the functionality is more consistent across different browsers with the new plug-in.
  • Increasing the functionality. It's now possible to instantiate new Java objects from JavaScript, call methods on them, as well as reference third-party packages on all browsers.

Calling Java from JavaScript

The following code shows how to invoke methods on an applet through JavaScript:

Java:

package org.example;

public class MyApplet extends Applet {
     public String getMessage() {
          return "Hello World!";
     }
}

HTML/JavaScript:

<applet id="myApp" archive="helloworld.jar" code="org.example.MyApplet"/>

<script type="text/javascript">
     var msg = myApp.getMessage();
     alert(msg);
</script>

Public fields in the Java class can also be called from JavaScript, as well as static fields and methods. Due to a new synthetic keyword, Packages, that is now attached to every applet, it is now possible to instantiate/call methods on classes that are not applets. For example, if we add a new class in the org.example package,

package org.example;

public class MyNonApplet {
     public String getOtherMessage() {
          return "This is NOT a hello world!";
     }
}

we can now create an instance of this class from the JavaScript and do things with it. Using the example above, let's modify the script a little:

<script type="text/javascript">
     var other = new myApp.Packages.org.example.MyNonApplet();
     alert(other.getOtherMessage());
</script>

This opens up the intriguing possibility of accessing a rich set of Java APIs from client-side JavaScript. Take, for example, the ability to add SVG support in (ahem) the only major browser that does not support it*. With Apache Batik and the Java plug-in, full support of SVG in the browser--SVG JavaScript API and all--becomes a reality. (Batik 1.7, by the way, is one of the most compliant implementations of SVG. And SVG might just be a faster way into the web designer's heart than JavaFX, but I digress...)

Each time a call into an applet is made from JavaScript, it is handled by a worker thread that is associated with the applet (there is only one per applet).

Calling JavaScript from Java

All instances of JavaScript objects within Java code appear as instances of netscape.javascript.JSObject. There are essentially two ways of invoking JavaScript from Java:

  1. Calling the static JSObject.getWindow(Applet arg) method with the applet object passed in as an argument. This method returns a JSObject that represents the DOM Window. This JSObject can in turn be used to call() JavaScript functions or eval() JavaScript code. The API for JSObject is here.
  2. Add a method in your Java code that takes JSObject as a formal parameter. Communication can then be bootstrapped from the JavaScript side by calling this Java method and passing a JavaScript object as an argument. It's a little less convenient than option 1, but useful if some JavaScript code wants to delegate some work to the Java code.

Furthermore, you can actually modify the HTML DOM from the Java side using a common DOM API. For the purposes of compiling Java code written with references to JSObject, you would place the jre/lib/plugin.jar on your classpath. Note also that, since JavaScript is a single-threaded language, communication from Java to JavaScript is restricted to one thread at a time; so you should structure your code accordingly.

Other JVM Languages

Of course, the other interesting possibility is the idea of languages like Scala, JRuby, Groovy, Clojure, or Fan being able to run in the browser with support for JavaScript interaction. A truly sick mind (and you know who you are) might use the applet merely as a trojan horse in order to get his/her favorite language running in the browser. I'll leave it at that!

Sun's renewed interest in the applet--sparked in part by RIA and in no small way by the resurgence of alternative browsers--has opened a door many thought closed a long time ago. Interesting times, indeed.

* Even worse now that the Adobe SVG plug-in has been discontinued.

Browser Wars II: The Rebels Strike Back

Tagged:  

In the heady days of the mid-1990's, a company called Netscape Communications Inc. rode the wave of the emerging Internet excitement to technology stardom. With its flagship product, Netscape Navigator, controlling over 90% of the web browser market and a stock price that reflected that, the company seemed destined for greatness...but, alas, dreams can disappear quickly in the tech industry, and Netscape's dream was a particularly brutal example. Microsoft, sensing an opportunity in the emerging Internet as well as a potential threat to its mainstay desktop business, launched itself into the browser market in late 1995 with the release of Internet Explorer 1.0 (for Windows 95), based on code licensed from Mosaic. The first browser war had started in earnest.

WebKit Browser Passes Acid 3 Test

Tagged:  

The WebKit browser project, upon which the Google Chrome and Apple Safari browsers are based, has just announced that their browser has become the first browser to pass all of the Acid 3 tests.

The third revision of the Acid tests, promoted by the Web Standards Project as a way of ensuring browser compatbility with web standards, includes tests of the following standards:

DOM2: Core, Events, HTML, Range, Style, Traversal, Views
ECMAScript
HTML4
HTTP
Media Queries
Selectors (:lang, :nth-child(), combinators, dynamic changes, …)
XHTML 1.0
CSS2 (@font-face)
CSS2.1 (’inline-block’, ‘pre-wrap’, parsing…)
CSS3 Color (rgba(), hsla(), …), UI (’cursor’)
data: URIs
SVG

The Acid 3-passing version of WebKit is available as a download of the nightly build.

Code Snippet: Detect Google Chrome w/JavaScript

With Chrome entering the browser market, you may find a need in the not too distant future to code some exceptions for it. In case that day comes, here's a quick snippet of JavaScript for detecting if your users are Chromed out:

You can detect Chrome using the following:
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

Chrome's full user agent:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13

Google Retracts Objectionable EULA Language

Tagged:  

In all the excitement surrounding the Chrome release, a few people quickly noticed some disturbing language in the EULA agreement:

11.1 You retain copyright and any other rights that you already hold in Content that you submit, post or display on or through the Services.

As Matt Cutts of Google blogged, Google has now retracted that language from Chrome's EULA agreement.

You are now free to post blogs (including this one) without consulting Google's legal team. *Whew*.

Google Chrome

Yesterday, the official Google blog broke the news that, yes, Google is indeed developing its own browser called Google Chrome. Much of the motivation and thinking behind this move is described interesting enough in an online comic strip.

Early news:

  • It is based on WebKit, the open source engine behind Safari
  • The browser runs with a JavaScript virtual machine codenamed "V8" that was developed from scratch by a team in Denmark
  • Each tab in the browser will represent a separate process, and will have its own controls and address bar
  • Google's Gears project will be bundled with the browser
  • The browser will have multiple tabs and an auto-complete address bar (like Firefox)
  • There will be a "privacy mode" where no traces of browsing history are left on the computer
  • Web applications can be launched in their own window (sans address bar, etc), an "RIA" feature
  • Built in protection from phishing and malware

According to the blog entry the beta version of Chrome should be available sometime today at this page, but as of this writing the URL just returns a "Page Not Found" (404).

Update: Chrome is now available at the above link.

Firebug 1.2 Released

Tagged:  

Firebug, the latest version of the indispensable debugging utility for Firefox, has released version 1.2 of the plugin for Firefox 3.0.

What's New?

  • Selective Panel Enablement: You can now choose to enable any combination of Console (logging), Script (JavaScript debugging) and Net (Network monitoring).
  • Automatic activation of HTML, CSS, and DOM views when Firebug UI is visible/activated (since they require minimal overhead).
  • Ability to suspend/resume Firebug.
  • A new tooltip telling you which tab/page in Firefox has enabled Firebug.

John Resig, creator of jQuery and now lead on the Firebug team, gives an excellent overview on his blog. Resig points out the currently rather global nature of enabling certain Firebug features, the overhead of enabling them (especially Script, which slows down all JavaScript running in Firefox about 25%), and states that all of these issues are targets for the 1.3 release.

Use the Add-ons panel in Firefox to update your plugin or get it now.

The release notes are here.

Faster JavaScript in Firefox 3.1

Tagged:  

Mozilla plans to give some relief to web developers struggling to create a "desktop-like" experience for web applications: relief in the form of huge increases in speed. On Friday, the father of JavaScript, Brendan Eich, announced the launch of a new JIT (Just In Time) JavaScript compiler called TraceMonkey that will be included in Firefox 3.1.

Depending on the benchmark, TraceMonkey currently improves JavaScript performance in Firefox 3.1 by about 2 to 37 times over Firefox 3.0. The average performance improvement is predicted to be about 4.6 times faster.

TraceMonkey is an evolution of Mozilla's current JavaScript engine, SpiderMonkey, that also draws from the Adobe-contributed Tamarin Tracing project. It is based on the concept of tracing (or trace-based compilation), which takes the approach of monitoring bytecode interpretation, following frequently-executed backwards branches to a "loop start point", analyzing the linear sequence of instructions in what is called a "trace" (using data structures called Trace Trees), and natively compiling these code paths. This has the advantage over more traditional JIT compilers--that keep track of which methods get called most frequently, and do whole-method analysis and compilation--of having to analyze and natively compile only the performance-critical parts of the code. It also has the advantage of being more mobile-friendly as this translates to a much lighter memory footprint. Andreas Gal, the principal architect of the TraceMonkey project, discusses the technique in his blog.

According to Gal, Firefox now has the "fastest JavaScript engine in the world". So what can you do when your browser's JavaScript is that fast? How about online photo editing...

FireFox 3 Released Today!

Tagged:  

Today is the official launch date for Mozilla's Firefox 3! The new version of the browser promises quite a few new features include performance improvements.

Below is an excerpt of a few new features.

Improved Platform for Developers

  • New graphics and font handling: new graphics and text rendering
    architectures in Gecko 1.9 provides rendering improvements in CSS, SVG as well
    as improved display of fonts with ligatures and complex scripts.
  • Color management: (set gfx.color_management.enabled on in
    about:config and restart the browser to enable.) Firefox can now adjust images
    with embedded color profiles.
  • Offline support: enables web applications to provide offline
    functionality (website authors must add support for offline browsing to their
    site for this feature to be available to users).
  • A more complete overview of href="http://developer.mozilla.org/en/docs/Firefox_3_for_developers">Firefox 3
    for developers is available for website and add-on developers.

Improved Performance

  • Speed: improvements to our JavaScript engine as well as profile guided
    optimizations have resulted in continued improvements in performance.
    Compared to Firefox 2, web applications like Google Mail and Zoho Office
    run twice as fast in Firefox 3, and the popular
    SunSpider test from Apple shows improvements over previous releases.
  • Memory usage: Several new technologies work together to reduce the amount
    of memory used by Firefox 3 over a web browsing session.
    Memory cycles are broken and collected by an automated cycle collector, a new
    memory allocator reduces fragmentation, hundreds of leaks have been fixed, and
    caching strategies have been tuned.
  • Reliability: A user's bookmarks, history, cookies, and preferences
    are now stored in a transactionally secure database format which will prevent
    data loss even if their system crashes.

You can read all about the new features here.

I am just waiting until I can download the software (should be any moment) and I'd love to hear if you plan on downloading Firefox 3 or if you prefer a different browser.

Mozilla CEO says Apple's Safari auto-update 'wrong'

Tagged:  

Over at cnet's News.com they have reported the John Lilly the CEO of Mozilla has criticized Apple's auto-update practices.

Below is an excerpt from the post.

In a blog on Friday, Mozilla CEO John Lilly criticized Apple's practice, uncovered this week, of offering iTunes and QuickTime users Safari 3.1 on Windows through the Apple Software Update pop-up.

Lilly says that automatic updates are a good way to ensure people have the most recent and secure versions of software. It's a practice that Mozilla uses with the Firefox browser.

What's different in what Apple is doing is that it is adding a product to the auto-update list that users never requested. That means they could very easily install software unintentionally, he argued:

Apple has made it incredibly easy--the default, even--for users to install ride along software that they didn't ask for, and maybe didn't want. This is wrong, and borders on malware distribution practices.

It's wrong because it undermines the trust that we're all trying to build with users. Because it means that an update isn't just an update, but is maybe something more. Because it ultimately undermines the safety of users on the Web by eroding that relationship. It's a bad practice and should stop.

You can read the full post here.

I would love to hear your thoughts on this practice (You can leave them in the comments or write a post about it on this blog using your free Ajaxonomy account).

Syndicate content