Dynamically Getting JavaScript Properties

July 23rd, 2008 by Unknown Morphician

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...

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Fark
  • LinkedIn
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • RSS
  • email

One Response to “Dynamically Getting JavaScript Properties”

  1. Kim says:

    Probably less obvious, but you can make use of the toString method of any in-page property.
    This works directly from flash without the need to setup any JS methods in the page beforehand.

    ExternalInterface.call( “window.location.href.toString” );

Leave a Reply