Dynamically Getting JavaScript Properties

July 23, 2008 – 10:44 pm by Sam Ahn

Jumping off of Joey’s post about anonymous functions from externalinterface, here’s a quick and perhaps obvious method of dynamically getting JavaScript properties:


package examples {

import flash.external.ExternalInterface;

public class JavaScript {

    public static function getProperty(name:String):Object {
        var property:Object;
        if (ExternalInterface.available) {
            property = ExternalInterface.call("function() { return " + name +"; }");
        } else {
            property = new Object()
        }
        return property;
    }

    public function JavaScript() {}
}
}

The getProperty() method creates a dynamic anonymous function that simply returns a value.
With such a utility, you can easily access JavaScript properties like so:


trace(JavaScript.getProperty("window.location.href"));

Who needs the FABridge? Just kidding…

Post a Comment