是否可以使用Flash / AS3获取URL变量?

时间:2009-09-02 06:33:41

标签: flash url variables

当用户点击链接时,会将其重定向到某个页面,例如:

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

在AS3中可以获取变量(var)吗?

(我知道有很多方法可以获得变量,例如,php $ _GET,但我的网站纯粹基于flash,我不想使用php获取值并将其存储在会话中并将其传递给其他页面。我无法以表格形式存储并将其传递给其他页面,因为主按钮位于Flash中,因此我需要使用AS3传递变量。)

5 个答案:

答案 0 :(得分:2)

import flash.external.ExternalInterface;

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

此调用要求JS为您提供当前页面网址。

然后,您可以解析它以提取变量。 http://manfred.dschini.org/2008/05/12/as3-url-class/上的示例。

答案 1 :(得分:1)

使用FlashVars作为<object>代码中的参数,查看this tutorial。基本上,您可以使用PHP或Javascript从地址字符串中获取变量,然后将它们插入到<param> FlashVars标记中。

答案 2 :(得分:1)

我是第二个@Daniels。您可以使用此类来解析字符串并将其放在易于调用的对象中。

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.");
            }
        }
    }
}

然后

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

取自本网站

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

答案 3 :(得分:1)

这是我用来将GET查询字符串传递给我的AS3。

首先,这是主要时间轴中从PHP收集FlashVars的行为(来自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

}

然后这是PHP(也是从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)是一种将URL查询字符串转换回查询字符串以供Flash使用的便捷方法。

我更喜欢这种方法,因为它不需要任何黑客攻击或导入类来完成这个简单的任务。

答案 4 :(得分:1)

这就是你需要的。

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;
    }
}

在你的例子中使用(www.domain.com/index.php?var=string)写:

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