JavaScript 函数可以检测哪个 Flash DOM 对象调用了它吗?
挑战如下:我有一个 Flash 影片,它将使用未知的 DOM ID 嵌入到页面中,我希望能够在 JS 函数中识别/存储回调。
我理想的用户流程是:
- 用户单击 Flash 中的按钮。
- Flash 暂停任何动画/视频/声音/等。Flash
- 调用注入的 JS 函数来显示页面覆盖的叠加体验。
- 当用户关闭叠加体验时,将调用 Flash 对象上的回调方法。
- Flash 恢复播放。
问题是,当AS3使用ExternalInterface.call("functionName", args...)方法时,似乎没有触发DOM事件,因此无法判断哪个对象调用了JS函数,所以有一个“registerMe()”函数似乎不起作用。基本上,注入的JS函数没有办法确定调用哪个DOM对象,因为Flash对象的ID是未知的。
[更新] 事实证明,SWF 可以使用 loaderInfo.url 确定自己的 url。我将该信息传递给启动叠加体验的脚本,以便可以存储它以便将来与所有 application/x-shockwave-flash DOM 对象进行比较。当找到匹配项时,即调用 SWF。有人看出这个逻辑有缺陷吗? (我对 JS 的熟练度不如对 AS 的熟练程度)
Here's the challenge: I have a Flash movie which will be embedded in a page using an unknown DOM ID that I want to be able to identify/store for callback in a JS function.
My ideal user flow would be:
- User clicks button in Flash.
- Flash pauses any animations / video / sounds / etc.
- Flash calls an injected JS function to display a page-covering overlay experience.
- When user closes overlay experience, a callback method on the Flash object is called.
- Flash resumes playback.
The problem is, when AS3 uses the ExternalInterface.call("functionName", args...) method, there doesn't seem to be a DOM event triggered, and thus it is impossible to tell which object called a JS function, so having a "registerMe()" function doesn't seem to work. Basically, the injected JS function has no way to determine which DOM object to call, because the ID of the Flash object is unknown.
[UPDATE]
It turns out, a SWF can determine its own url using loaderInfo.url. I'm passing that information to the script that launches the overlay experience so it can be stored for a future comparison against all application/x-shockwave-flash DOM objects. When a match is found, that is the calling SWF. Does anyone see a flaw in this logic? (I'm not nearly as proficient with JS as I am with AS)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你不能。我能想到的唯一“干净”的方法是通过Flashvars注入Flash对象的ID。
丑陋的方法是找到所有 Flash 对象并使用 loaderInfo .url 来比较并识别正确的 Flash 对象。
I don't think you can. The only "clean" way I can think of is to inject the ID of the Flash object through Flashvars.
The ugly way would be to find all Flash objects and use loaderInfo.url to compare and identify the correct Flash Object.
因为我假设您在这里可以完全控制整个嵌入过程,所以听起来您可以在嵌入时将其 DOM ID 作为 flashvar 告诉 SWF(无论是通过 SWFObject 之类的 Javascript 还是在服务器生成 HTML 时)。然后,当它调用 Javascript 函数来触发该接口时,它可以将其 DOM ID 作为参数发送。
并不完全理想,但在浏览器上绝对可行且简单。
Since I'm assuming that you have full control over the entire embedding process here, sounds like you could tell the SWF its DOM ID as a flashvar as it's being embedded (whether that's via Javascript like SWFObject or as the server generates the HTML). Then, when it calls the Javascript function to trigger the interface, it can send its DOM ID as an argument.
Not exactly ideal, but definitely feasible and easy on the browser.
您正在调用的 JavaScript 函数是手动调用的,而不是作为事件调用的。就像在 JS 中使用
call
或apply
方法一样。您可以做的是传递活动 Flash 视频的 DOM 名称/ID 作为您正在调用的函数的参数,以便它知道要引用哪个 DOM 元素:
此方法的一个“问题”是您需要确保
object
和/或embed
元素同时设置了[id]
和[name]
属性,因为ExternalInterface.objectID
在不同浏览器中的注册会不一致。如果我没记错的话,IE 读取
[name]
,而 ff/chrome/opera/safari 读取[id]
,尽管我相信一些浏览器会成功回退到<代码>[名称]。我需要做一个测试来确认这一点。无论如何,给出 相同的
name
和id
应该可以正常工作(您将能够仅根据 ID 选择 DOM 中的元素) 。The JavaScript function you are calling is being called manually, not as an event. It's just like if you used the
call
orapply
methods in JS.What you can do is pass the DOM name/ID of the active flash video as a parameter to the function you're calling, so that it knows which DOM element to refer back to:
One "gotcha" with this method is that you need to make sure the
object
and/orembed
elements have both their[id]
and[name]
attributes set, becauseExternalInterface.objectID
will be registered inconsistently across browsers.If I remember right, IE reads the
[name]
and ff/chrome/opera/safari read the[id]
, although I believe a couple browsers would fallback successfully to the[name]
. I will need to do a test to confirm this.In any event, give an identical
name
andid
and it should work fine (you'll be able to select the element in the DOM just based on the ID).