Rella
Rella

Reputation: 66935

Actionscript 3, flex: How to find out which is browser you are currently in?

I need to detect via AS3 inside flex mxml appication which is browser I am in - FF, Chrome, IE etc, only name and version. How to do such thing?

Upvotes: 0

Views: 2749

Answers (3)

Onkar Nene
Onkar Nene

Reputation: 1379

As per my research navigator.appName will return string Netscape for browsers like: IE11, Firefox, Chrome and Safari. check this.

If you want to detect browser name try this:

var browserName:String;
var userAgent:Object = ExternalInterface.call("window.navigator.userAgent.toString");

if(userAgent.toLowerCase().indexOf("chrome") !== -1)
{
browserName = "Chrome";
}

if(userAgent.toLowerCase().indexOf("mozilla") !== -1)
{
browserName = "Mozilla";
}

if(userAgent.toLowerCase().indexOf("safari") !== -1)
{
browserName = "Safari";
}

reference : check visitor OS & Browser using as3

Upvotes: 0

Nate
Nate

Reputation: 2881

Yes, you're going to use javascript, but you dont actually need to put javascript in the page.

Here's a quick script example of getting that information from your flex app without adding anything to the containing html page:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
          creationComplete="creationCompleteHandler(event)">
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        protected function creationCompleteHandler(event:FlexEvent):void
        {
            var appName : String  = String(ExternalInterface.call("function(){return navigator.appName}"));
            var appVersion : String  = String(ExternalInterface.call("function(){return navigator.appVersion}"));
            var userAgent : String  = String(ExternalInterface.call("function(){return navigator.userAgent}"));
            trace( appName ) ;
            trace( appVersion );
            trace( userAgent );
        }

    ]]>
</fx:Script>

This traces the info out to the console, so for example when I run it I get :

Microsoft Internet Explorer
4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB6.6; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB6.6; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)

Let me know if you have any issues!

Upvotes: 8

Alex
Alex

Reputation: 4934

It's not tricky if you use some client side JavaScript and the ExternalInterface class in the Flash library.

Here's a brief tutorial on doing that: http://codingrecipes.com/calling-a-javascript-function-from-actionscript-3-flash

Your JS would need to run a browser client check.

Upvotes: 1

Related Questions