views:

178

answers:

1

I want to know the type and version the browser that the user is running from within my Flex 4 application. I know I can get that information by using ExternalInterface to call Javascript. I know I can get that information from the server.

I'm looking for a way to get that information directly from actionscript. I mean, isn't there a global variable or something that keeps this information?

+2  A: 

You can't since you don't have any global variables as you mention.

But whty not use ExternalInterface and JavaScript?.

var method:XML = <![CDATA[
     function( ){ 
         return { appName: navigator.appName, version:navigator.appVersion};}
    ]]>

var o:Object = ExternalInterface.call( method );
trace( "app name ",o.appName,"version ", o.version )

If you put it in a class as a static method, for you it would be as transparent as calling an intrinsic class...

package {
    import flash.external.ExternalInterface;


    public class BrowserUtils {

        private static const CHECK_VERSION:XML = <![CDATA[
             function( ) { 
                return { appName: navigator.appName, version:navigator.appVersion };
                }
            ]]>;

        public static function getVersion( ):Object {
            if ( !ExternalInterface.available ) return null;            

            return ExternalInterface.call( CHECK_VERSION );
        }

    }

}
goliatone
How come the JavaScript in the XML gets executed in the browser? It seems to work, but I don't get why it works :)
Lars
@Lars: the function gets executed in the browser because of the ExternalInterface.call() function is used to call it.@goliatone, thanks for the detailed answer, I'll probably do this. I was just wondering if there was already a global variable that I could use, but couldn't find in the documentation.
Jaffer
@Lars: Basically, external interface will try to execute any string you pass to it. I wrapped the method in a cdata xml tag just so i can create that string in a comfortable manner in my editor.@jaffer: I understand, it would be most convenient that way.
goliatone