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

2023-09-08 14:55:17 作者:心会凉i

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

这是我到目前为止所。它不工作,但并htmlComponent.domWindow是迄今为止空:

 如果(htmlComponent == NULL){
    htmlComponent =新的HTML();
    htmlComponent.htmlText =< HTML><脚本>中+ myJavaScript +< / SCRIPT><身体GT;< / BODY>< / HTML>中;
    htmlComponent.addEventListener(完全,功能(五:事件):无效{跟踪(HTML载入完成');});
    IInvalidating(htmlComponent).validateNow();
}

如果(htmlComponent.domWindow&安培;&安培; htmlComponent.domWindow.validateXML){
    结果= htmlComponent.domWindow.validateXML(值);
}
 

解决方案 做智电时代最舒适的汽车品牌,飞凡汽车靠的是什么

您的问题,很可能是你不等待内容的加载实际加载。所以,你的code运行时,DOM仍是空的。

  VAR的htmlText:字符串=< HTML><脚本>中+ myJavaScript +< / SCRIPT><身体GT;< / BODY>< / HTML>中;
htmlLoader的=新的HTMLLoader();
htmlLoader.loadString(的htmlText);

//添加一个完整的处理程序,并没有引用DOM,直到它完成
htmlLoader.addEventListener(引发Event.COMPLETE,loadComplete);

功能loadComplete(五:事件):无效{
    如果(htmlLoader.domWindow&安培;&安培; htmlLoader.domWindow.myFunction){
       //...do你需要与存在domWindow
    }
}
 

What is the easiest way I can run JavaScript dynamically in a Flex or ActionScript AIR application?

This is what I have so far. It does not work yet and htmlComponent.domWindow is so far 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.

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