views:

1448

answers:

3

Hi there.

I'd like to know what Browser the User is using to view my Flex application. How can I get at the User Agent string and other information?

+4  A: 

Javascript can help you with browser detection and figuring out UserAgent. Use ExternalInterface to make the Javascript talk to your flex application. Here's an ExternalInterface tutorial.

dirkgently
Is there a way to do it in Flex, though? Or is the EI route my only option?
DyreSchlock
AFAIK, there is no inbuilt Flex class to retrieve browser data directly. Also take a look at this example <http://digitalmedia.oreilly.com/helpcenter/flex3cookbook/chapter20.html> I'm inclined to think JS + AS is the way to go.
dirkgently
It wouldn't make sense to expose it in Flex, since Flex applications aren't necessarily hosted in a browser.
Tmdean
+1  A: 

Your javascript:

function determineBrowser()
{
    // do whatever browser checks you prefer here, then return
    // a value (a string would probably work best) that will indicate
    // to your flash what browser it is

    // I'm just gonna copy and paste an extremely
    // simple one for example purposes

    if(navigator.appName == "Netscape")
    {
        return "Netscape";
    }
    if(navigator.appName == "Microsoft Internet Explorer")
    {
        return "Internet Explorer";
    }

    return "Not IE or Netscape";
}

Your Actionscript:

import flash.external.ExternalInterface;

var browser: String = ExternalInterface.call("determineBrowser");

Using what I did, whatever your javascript function returns is what the browser variable in actionscript will be, so you can get any browser data you need as long as you make the javascript determine it.

I recommend making it a little more robust than I did, but I just wanted to give you the basic idea in short enough terms to be easy to digest!

Bryan Grezeszak
+2  A: 

Hello,

you can 'embed' your javascript inside AS3 code like this :

var v : String = ExternalInterface.call("function(){return navigator.appVersion+'-'+navigator.appName;}");
var t : TextField = new TextField();
t.autoSize = TextFieldAutoSize.LEFT;      
addChild(t);
t.text = v;

the textField will display infos about the navigator like this (chrome):

5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.53 Safari/525.19-Netscape

OXMO456
that's not a bad idea.
DyreSchlock