开发者

Can an embedded Flash object access the DOM of its parent document?

I'm just curious开发者_开发百科 if it's possible that Flash objects can access the DOM of the document that has embedded it.


Yes, through the ExternalInterface class.

You can make calls to Javascript from within the Flash movie and get back any public information about the page that your heart desires.

Addendum

Looking at this a year and a half later, i decided to add some examples:

Say you have a JS function on your client page like this:

function foo(bar,type) {
  // do something with bar and type
}

You call it from Flash (using AS3) like so:

ExternalInterface.call(foo, bar, type);

Note that the function name is the first object and the arguments are listed sequentially thereafter.

To expose a method of the Flash movie to outside Javascript, you would do this in your Flash or Flex (again, AS3):

application1_applicationCompleteHandler(event:Event) {
  // the app has finished loading, so do whatever we
  // have to do on load, plus add that callback
  ExternalInterface.addCallback(foo, bar);
}

public function bar(arg1, arg2) : void {
  // do something with arg1 and arg2
}

In the Javascript on the page you invoke it like this (where myMovie is the ID of the SWF):

myMovie.foo(anArg, anotherArg);

In the addCallback method, the first argument is the external name of the function, and the second argument is the closure that will get called.


Yes.

An example: http://livedocs.adobe.com/flex/3/html/help.html?content=ProgrammingHTMLAndJavaScript_07.html


Not that I know of, but they can execute javascript in the containing document, which obviously can then access the DOM itself.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜