简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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