积少成多Flash(8) - ActionScript 3.0 网页之获取参数,JavaScript与ActionScript之间的相互调用

[索引页]
[源码下载]


积少成多Flash(8) - ActionScript 3.0 网页之获取参数,JavaScript与ActionScript之间的相互调用


作者:webabcd


介绍
演示使用 Flash ActionScript 3.0 获取配置参数,页面参数,以及 js 与 as 间的交互 
  • 参数获取 - 通过 Application.application.stage.loaderInfo.parameters[param] 获取配置参数(flashVars);通过 JavaScript 获取 url 参数
  • 与JavaScript交互 - 利用 ExternalInterface 实现 ActionScript 和 JavaScript 间的相互调用


Flash 端
Main.as
/*
* 注:    
* 1、如果需要 Flash 与 JavaScript 交互,那么包含 swf 文件的 html 标记必须要有 id
* 2、如跨域调用,需要设置相关的插件参数,如 <param name="allowScriptAccess" value ="always" />
* 3、Flash端的相关安全类配置:类似 flash.system.Security.allowDomain("*");
*/
package    
{
        import flash.display.MovieClip;
        import flash.external.ExternalInterface;

        public class Main extends MovieClip
        {
                public function Main():void
                {
                        loadVarsDemo();
                        
                        asCallJS();
                        
                        jsCallAS();
                }
                
                
                // 获取页面端配置的参数
                // 下面的示例用于读取页面端的如下配置    
                // <param name="flashVars" value="name=webabcd&age=29" />
                // <param name="movie" value="Demo.swf?dateOfBirth=1980-02-14" />
                private function loadVarsDemo():void
                {
                        lblVars.text =    
                                "姓名:" + stage.loaderInfo.parameters["name"]
                                + "    "
                                + "年龄:" + stage.loaderInfo.parameters["age"]
                                + "    "
                                + "生日:" + stage.loaderInfo.parameters["dateOfBirth"];
                                
                        lblSwfUrl.text = stage.loaderInfo.url;
                }
                
                
                // ActionScript 调用 JavaScript
                private function asCallJS():void
                {
                        // 调用指定的 JavaScript 端的函数,并顺序指定传递给 JavaScript 端函数的参数
                        ExternalInterface.call("jsMethod", "param1", "param2");
                }
                
                
                // JavaScript 调用 ActionScript 时,ActionScript 端的函数
                private function jsCallAS():void
                {
                        // 注册一个需要被 JavaScript 调用的函数
                        ExternalInterface.addCallback("asMethod", output);        
                }
                
                public function output(p1:String, p2:String):void
                {        
                        lblResult.text = "参数1:" + p1 + " 参数2:" + p2
                }    
        }
}
 
Web 端
Demo.html
< html xmlns ="http://www.w3.org/1999/xhtml" xml:lang ="zh_cn" lang ="zh_cn" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title >Demo </title>
< script language ="javascript" >AC_FL_RunContent = 0; </script>
< script src ="AC_RunActiveContent.js" language ="javascript" > </script>
</head>
< body bgcolor ="#ffffff" >

< script type ="text/javascript" >

        Request = {
                // 根据指定的名称取 url 参数值
                QueryString : function(name){
                     var svalue = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]*)(\&?)","i"));
                     return svalue ? svalue[1] : svalue;
                },
                
                // 取 url 参数字符串
                Query : function(){
                     var svalue = location.search.match(new RegExp("[\?](.*)","i"));
                     return svalue ? svalue[1] : svalue;
                }
        }
        
        // 根据指定的名称取 url 参数值
        alert(Request.QueryString("param"));
        
        // 取 url 参数字符串
        alert(Request.Query());
        
</script>

< script type ="text/javascript" >

        // ActionScript 调用 JavaScript 时,JavaScript 端的函数
        function jsMethod(p1, p2)
        {
                alert("参数1:" + p1 + " 参数2:" + p2);
        }

        // JavaScript 调用 ActionScript
        window.onload = function()
        {
                var swfObj = document.getElementById('Demo');        
        
                // asMethod 为 ActionScript 端注册的可被 JavaScript 调用的函数
                swfObj.asMethod("param1", "param2");
        }
        
</script>

< 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 ="550" height ="400" id ="Demo" align ="middle" >
         < param name ="allowScriptAccess" value ="sameDomain" />
         < param name ="allowFullScreen" value ="false" />
         < param name ="movie" value ="Demo.swf?dateOfBirth=1980-02-14" />
         < param name ="quality" value ="high" />
         < param name ="bgcolor" value ="#ffffff" />        
         < embed src ="Demo.swf" quality ="high" bgcolor ="#ffffff" width ="550" height ="400" name ="Demo" align ="middle" allowScriptAccess ="sameDomain" allowFullScreen ="false" type ="application/x-shockwave-flash" pluginspage ="http://www.macromedia.com/go/getflashplayer" />
         < param name ="flashVars" value ="name=webabcd&age=29" />
</object>

</body>
</html>
 
 
OK
[源码下载]

你可能感兴趣的:(JavaScript,Flash,网页,actionscript,积少成多)