AS ideas

A new idea means a new world.

导航

可以把javaScript嵌入到swf里了

Posted on 2009-09-29 11:35  凤城14郞  阅读(234)  评论(0编辑  收藏  举报
flex 开发者应该对使用Embed元数据标签嵌入各种资源都很熟悉了,现在在flash cs4中同样可以使用元数据标签进入嵌入资源了
  好多人应该还不知道竟然可以将javaScript库嵌入到swf从而使在swf可以写html DOM,而最终部署的时候这个js文件不需要暴露出来
  这个技术其实很简单,基础就是用[Embed()]标签将js文件嵌入到库中,然后声明一个Class绑定到该资源,在实例化的时候将该引用的字符串发送javaScript的eval声明就可以,以下是一个简单的实例:
JavaScript:

      
function hello()
      
{
           alert(
"hello");
      }

package
{
        import flash.display.Sprite;
        import flash.external.ExternalInterface;
        public class EmbeddedJavaScriptExample extends Sprite
        
{
                [Embed(source
="hello.js", mimeType="application/octet-stream")]
                private static const HelloJS:Class;
                
                public 
function EmbeddedJavaScriptExample()
                
{
                        execute();
                        

                }

                public 
function execute():void{
                        
if (ExternalInterface.available)

            
{

                
// embed the JavaScript to the page

                ExternalInterface.call(
"eval"new HelloJS().toString());

               

                
// the embedded JavaScript has a function call named hello

                
// now that it has been embedded to the page call it

                ExternalInterface.call(
"hello");

            }

                }


        }

}