开发者

Flex ExternalInterface : possible for ActionScript to interrogate the HTML document?

开发者 https://www.devze.com 2022-12-19 17:57 出处:网络
Is there a way for the Flex a开发者_开发技巧pp to get any information about the HTML document via the ExternalInterface? Can Flex find out if a particular DIV exists, for example?ExternalInterface is

Is there a way for the Flex a开发者_开发技巧pp to get any information about the HTML document via the ExternalInterface? Can Flex find out if a particular DIV exists, for example?


ExternalInterface is a Javascript interface with the containing page, so anything that is possible in javascript is possible through ExternalInterface, either directly or through functions on the hoast page.

AnnonymousFunctionCall from ExternalInterface


Is there a way for the Flex app to get any information about the HTML document via the ExternalInterface?

Check out JSInterface - JavaScript API for ActionScript 3. Two front page examples are called:

  • JavaScript'ing Inside Flash
  • ActionScript'ing Inside HTML

I use this library currently and it is extremely powerful. I asked him (Oleg Galaburda) once whether or not I should use this for simply being able to resize the swf. He said something like "if you only need to do a few in javascript, just stick with ExternalInterface. If you need access to the DOM in ActionScript, use this".

There are hundreds of examples in the svn repository. He's done an awesome job with this thing. It converts JavaScript Objects to ActionScript and back, so you can use full-on classes. It would take a ton of work to rebuild that so it's cross browser and everything. He's done that!

Everything in JSInterface is basically a dynamic class, so you can drill down the DOM easily. Here's some sample methods (in ActionScript):

protected function testCSS():void
{
    var styleTag:JSHTMLElement = JSInterface.pushCSS('.text{font-weight:bold;color:#ff00ff;font-size:90;text-decoration:underline;}');
    var font:JSHTMLElement = JSInterface.document.createElement('font');
    font.innerHTML = 'Hello world!';
    font.setAttribute('class', 'text');
    font.className = 'text';
    JSInterface.document.body.appendChild(font);
    trace(styleTag); 
}                   

protected function insertIFrame():void
{   
    var body:JSDynamic = JSInterface.document.getElementsByTagName('body')[0];
    var frame:JSDynamic = JSInterface.document.createElement("iframe");
    frame.width = 300;
    frame.height = 300;
    if (JSInterface.navigator.appName.indexOf('Microsoft')>=0)
    {
        frame.onreadystatechange = function():void
        {
            trace(frame.readyState);
        };
    }
    else
    {
        frame.onload = function():void
        {
            trace('iFrame loaded');
        };
    }
    frame.src = 'http://actualwave.com';
    body.appendChild(frame);            
}          

The library internally uses ExternalInterface to handle everything. But to serialize/deserialize the DOM to/from ActionScript, that's a ton of work. This library should do the trick.

Hope this helps, Lance


Responding to another question I explained how ExternalInterface actually works. What you can take from that is a simple "yes".

[edit] some sample code:

var jsFunc:String = 'function () { return document.getElementById("test") != null; }';
var divExists:Boolean = ExternalInterface.call(jsFunc);

should clarify things ...

0

精彩评论

暂无评论...
验证码 换一张
取 消