简体   繁体   English

如何在Flex AIR应用程序中动态运行JavaScript?

[英]How to run JavaScript dynamically in a Flex AIR application?

What is the easiest way I can run JavaScript dynamically in a Flex or ActionScript AIR application? 在Flex或ActionScript AIR应用程序中动态运行JavaScript的最简单方法是什么?

This is what I have so far. 到目前为止,这就是我所拥有的。 It does not work yet and htmlComponent.domWindow is so far null: 尚无法使用,并且htmlComponent.domWindow到目前为止为null:

if (htmlComponent==null) {
    htmlComponent = new HTML();
    htmlComponent.htmlText = "<html><script>"+myJavaScript+"</script><body></body></html>";
    htmlComponent.addEventListener("complete", function(e:Event):void {trace('html load complete');});
    IInvalidating(htmlComponent).validateNow();
}

if (htmlComponent.domWindow && htmlComponent.domWindow.validateXML) {
    result = htmlComponent.domWindow.validateXML(value);
}

Your issue, is likely that you do not wait for the loading of the content to actually load. 您的问题很可能是您没有等待内容的实际加载。 So when your code runs, the DOM is still empty. 因此,当您的代码运行时,DOM仍然为空。

var htmlText:String = "<html><script>"+myJavaScript+"</script><body></body></html>";
htmlLoader = new HTMLLoader();
htmlLoader.loadString(htmlText);

// add a complete handler and don't reference the DOM until it's complete
htmlLoader.addEventListener(Event.COMPLETE, loadComplete);

function loadComplete(e:Event):void {
    if (htmlLoader.domWindow && htmlLoader.domWindow.myFunction){
       //...do what you need to with the domWindow
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM