开发者

Extract the contents of a div using Flash AS3

开发者 https://www.devze.com 2023-03-22 00:41 出处:网络
I have a SFW embedded in a PHP page. There is also a div on the page with id=\"target\". I want to access the content of that div (ie: the characters inside it) and hold them as a String variable in

I have a SFW embedded in a PHP page. There is also a div on the page with id="target".

I want to access the content of that div (ie: the characters inside it) and hold them as a String variable in AS3. How can I do this?

My attempt so far

import flash.external.ExternalInterface;
var myDivContent = ExternalInterface.call("function(){ return document.GetElementById('target');}")开发者_运维技巧;
var myDivContent2:String = myDivContent.toString();
test_vars.text = myDivContent2; //Dynamic text output


I don't think you can define a function in the ExternalInterface.call() method. You have to call a function by name which already exists in the JavaScript.

So I'd create some JavaScript code like this:

function getTargetContent()
{
    return document.getElementById('target').innerHTML;
}

And then in your Flash,

var myDivContent = ExternalInterface.call("getTargetContent");

Note that document.getElementById('target') only returns the reference to that div, not the contents within. So if you don't return .innerHTML then the Flash will get an object which may not be usable (although I haven't actually tried doing this).


The easiest way to do this is as Allan describes, write a Javascript function to sit on the page and return the required value to you.

Of course, if you can't edit the page content, only the flash, then you do need to pass the function itself, which will actually have to be forced into the page though JavaScript injection. An example for your case, which I have not tested:

//prepare the JavaSctipt as an XML object for Dom insertion
var injectCode:XML = 
    <script>
        <![CDATA[
            function() {
                getElementContent = function(elementID) {
                    return document.getElementById(elementID).innerHTML;
                }
            }
        ]]>
    </script>;

//inject code
ExternalInterface.call(injectCode);
//get contents of 'divA'
var divAContent:String = ExternalInterface.call('getElementContent','divA') as String;
//get contents of 'spanB'
var spanBContent:String = ExternalInterface.call('getElementContent','spanB') as String;


You're almost there :

var res : String = ExternalInterface.call("function(){return document.getElementById('target').outerHTML}");

If you only want the content of your target, use innerHTML instead of outerHTML.

0

精彩评论

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

关注公众号