kanayaki
kanayaki

Reputation:

Is it possible to get URL variable using Flash/AS3?

When a user click a link, it would redirect them to a page, something like:

www.domain.com/index.php?var=string

Is it possible in AS3 to grab the variable (var)??

(I know there are alot of ways to get the variable, for example, php $_GET, but my website is purely flash based, I dont want to use php to get the value and store it in session and pass it to others pages. ANd I could not store in form and pass it to others pages, because the main button is in Flash, so I need to use AS3 to pass the variable).

Upvotes: 1

Views: 12833

Answers (5)

CyberProdigy
CyberProdigy

Reputation: 999

This is what you need.

private function getParams():URLVariables
{
    var url:String = ExternalInterface.call('window.location.href.toString');
    var parts:Array = url.split("?");
    if(parts.length == 2)
    {
        var uv:URLVariables = new URLVariables(parts[1]);
        return uv;
    }
    else
    {
        return null;
    }
}

To use in your example (www.domain.com/index.php?var=string) write:

var params:URLVariables = getParams();
trace(params.var); // traces string

Upvotes: 1

user24601
user24601

Reputation: 287

This is what I used to pass a GET query string into my AS3.

First, here's the actions from the main timeline that gather the FlashVars from PHP (modified code from developphp.com):

// Claim the offset Number that spaces the text 
// fields so they are not right on top of each other
var offSet:Number = 18;
// Claim keyStr variable as a string
var keyStr:String;
// Claim valueStr variable as a string
var valueStr:String;
// Create the paramObj Object and set it to load parameters
// from the root level being sent in by FlashVars externally
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;

// Set Text Format Object, its attributes and values
var format:TextFormat = new TextFormat();
      format.font = "Verdana";
      format.size = 15;
// Create the loop that iterates over all of the variables you send in using FlashVars     
var i=0;
for (keyStr in paramObj) {   
       i++;
       valueStr = String(paramObj[keyStr]);      // This line gets the values of each variable sent in
       var myText:TextField = new TextField(); // Create a text field to read and access each var
       myText.defaultTextFormat = format; // Set the formatting into the text field properties
       myText.text = keyStr + " value is: " + valueStr; // Use the vars here, I place into text field waiting
       myText.y = offSet*i; // Before we place each text field on stage we offSet it
       myText.width = 380; // Set the width of the text field

       this.addChild(myText); // Now this line actually places the text field on stage

}

Then here's the PHP (also modified from developphp.com)

<?php
$vars = http_build_query($_GET);
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Editor</title>
<script language="javascript">AC_FL_RunContent = 0;</script>
<script src="AC_RunActiveContent.js" language="javascript"></script>
</head>
<body bgcolor="#333333">
<script language="javascript">
    if (AC_FL_RunContent == 0) {
        alert("This page requires AC_RunActiveContent.js.");
    } else {
        AC_FL_RunContent(
            'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
            'width', '400',
            'height', '400',
            'src', 'editor',
            'FlashVars', '<?php echo $vars; ?>',            
            'quality', 'high',
            'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
            'align', 'middle',
            'play', 'true',
            'loop', 'true',
            'scale', 'showall',
            'wmode', 'window',
            'devicefont', 'false',
            'id', 'editor',
            'bgcolor', '#ffffff',
            'name', 'editor',
            'menu', 'true',
            'allowFullScreen', 'false',
            'allowScriptAccess','sameDomain',
            'movie', 'editor',
            'salign', ''
            ); //end AC code
    }
</script>
<noscript>
    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="400" height="400" id="AS3_php_Var_into_flash" align="middle">
    <param name="allowScriptAccess" value="sameDomain" />
    <param name="allowFullScreen" value="false" />
    <param name="movie" value="editor.swf" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#ffffff" />    
    <param name="FlashVars" value="<?php echo $vars; ?>" />
    <embed src="editor.swf" FlashVars="<?php echo $vars; ?>" quality="high" bgcolor="#ffffff" width="400" height="400" name="editor" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
    </object>
</noscript>
</body>
</html>

http_build_query($_GET) is a handy way to convert the URL query string back into a query string for Flash's use.

I prefer this method because it doesn't require any hacks or importing of classes to accomplish this simple task.

Upvotes: 1

brenjt
brenjt

Reputation: 16297

I second @Daniels. You can use this class to parse the string and place it in a easy to call object.

package
{
    import flash.external.*;

    public class QueryString
    {
        private var _queryString:String;
        private var _all:String;
        private var _params:Object;

        public function QueryString(url:String='')
        {
            readQueryString(url);
        }
        public function get getQueryString():String
        {
            return _queryString;
        }
        public function get url():String
        {
            return _all;
        }
        public function get parameters():Object
        {
            return _params;
        }       

        private function readQueryString(url:String=''):void
        {
            _params = new Object();
            try
            {
                _all = (url.length > 0) ? url : ExternalInterface.call("window.location.href.toString");
                _queryString = (url.length > 0) ? url.split("?")[1] : ExternalInterface.call("window.location.search.substring", 1);
                if(_queryString)
                {
                    var allParams:Array = _queryString.split('&');
                    //var length:uint = params.length;

                    for(var i:int=0, index=-1; i < allParams.length; i++)
                    {
                        var keyValuePair:String = allParams[i];
                        if((index = keyValuePair.indexOf("=")) > 0)
                        {
                            var paramKey:String = keyValuePair.substring(0,index);
                            var paramValue:String = keyValuePair.substring(index+1);
                            _params[paramKey] = paramValue;
                        }
                    }
                }
            }
            catch(e:Error)
            {
                trace("Some error occured. ExternalInterface doesn't work in Standalone player.");
            }
        }
    }
}

And then

var myPath:QueryString = new QueryString("http://www.studiosedition.com/?page=articles");
trace(myPath.parameters.page);

Taken from this site

http://blog.studiosedition.com/2010/03/query-string-as3/

Upvotes: 1

Daniel
Daniel

Reputation: 685

import flash.external.ExternalInterface;

var urlStr:String = ExternalInterface.call('window.location.href.toString');

This call asks JS to give you the current page url.

You can then parse it to extract the variables. Example at http://manfred.dschini.org/2008/05/12/as3-url-class/.

Upvotes: 2

John Nicely
John Nicely

Reputation: 1034

Check out this tutorial on using FlashVars as a parameter in your <object> code. Basically, you could use PHP or Javascript to get the variables from the address string and then insert them into the <param> FlashVars tag.

Upvotes: 1

Related Questions