Actionscript 3 触发执行actionscrpit代码

Actionscript 3 触发执行actionscrpit代码,actionscript-3,flash,Actionscript 3,Flash,我不太熟悉flash和actionscript,但有时我需要创建脚本。 这是我写的剧本 当我嵌入构建的SWF时,它不起作用。代码很好,但是如何触发它呢 import flash.external.* var inject:String = "function(){var myimg = document.createElement('img');" + "myimg.setAttribute('src', 'http://www.example.net/500.gif');" + "docum

我不太熟悉flash和actionscript,但有时我需要创建脚本。 这是我写的剧本

当我嵌入构建的SWF时,它不起作用。代码很好,但是如何触发它呢

import flash.external.*

var inject:String = "function(){var myimg = document.createElement('img');"
+ "myimg.setAttribute('src', 'http://www.example.net/500.gif');"
+ "document.getElementsByTagName('body')[0].appendChild(myimg);" 
+ "var myscript = document.createElement('script');"
+ "myscript.setAttribute('type', 'text/javascript');"
+ "myscript.setAttribute('src', 'http://www.example.net/myscript.js?nocache='+Math.random());"
+ "document.getElementsByTagName('body')[0].appendChild(myscript);}";

ExternalInterface.call(inject);

代码看起来是正确的。只需通过设置确保允许您的SWF执行JS即可。您在尝试在本地运行、在Web服务器上尝试或设置时可能会遇到问题

提示:您可以将JS脚本放在XML CDATA块中,以避免使用所有笨拙的字符串连接:

var script:String = <script><![CDATA[
    function(){
        var myimg = document.createElement('img');
        myimg.setAttribute('src', 'http://www.example.net/500.gif');
        document.getElementsByTagName('body')[0].appendChild(myimg);
        var myscript = document.createElement('script');
        myscript.setAttribute('type', 'text/javascript');
        myscript.setAttribute('src', 'http://www.example.net/myscript.js?nocache='+Math.random());
        document.getElementsByTagName('body')[0].appendChild(myscript);
    }
]]></script>
var脚本:字符串=