2015-08-28 64 views
0

什么是我可以在Flex或ActionScript AIR应用程序中动态运行JavaScript的最简单方法?如何在Flex AIR应用程序中动态运行JavaScript?

这是我到目前为止。这还没有工作,htmlComponent.domWindow是迄今为止空:

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

回答

1

您的问题,很可能是你不等待内容的加载实际加载。所以当你的代码运行时,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 
    } 
} 
+1

或者,你可以使用'StageWebView'或StageWebViewBridge https://code.google.com/p/stagewebviewbridge/ – BadFeelingAboutThis

+0

我不使用Flex(或HTML组件),这样做是否此评论做你需要的, – BadFeelingAboutThis

+0

我做了一些测试,它看起来像HTML组件需要被添加到舞台上才能工作。一旦我完成了“完成”事件,然后我就可以访问和运行JavaScript方法。我将其更改为使用HTMLLoader,并且不需要将其添加到舞台上。我只需要等待完整的事件。没有尝试过dom初始化事件。如果你想我可以用我使用的更新代码更新你的答案。 –

相关问题