如何动态地在Flex AIR应用程序运行JavaScript? [英] How to run JavaScript dynamically in a Flex AIR application?

查看:166
本文介绍了如何动态地在Flex AIR应用程序运行JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是我可以在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
    }
}

这篇关于如何动态地在Flex AIR应用程序运行JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆