在ExternalInterface中传递回调

发布于 2024-09-25 05:18:38 字数 411 浏览 3 评论 0原文

我想从 Flash 调用 Javascript 函数,可以使用 ExternalInterface 来实现,但 Javascript 函数需要回调。有没有办法给它一个Flash回调?

我想过这样的事情:

ExternalInterface.addCallback("foo", function(){...});
ExternalInterface.call("theFunction", "foo");

但这行不通,因为 theFunction 会尝试执行 foo(),而它实际上应该执行 swfObject.foo ()。问题是该页面及其 Javascript 不在我的控制之下(尽管如果确实需要,我可以请求更改)。

I want to call a Javascript function from Flash, which I can do with ExternalInterface, but the Javascript function takes a callback. Is there a way to give it a Flash callback?

I've thought of something like this:

ExternalInterface.addCallback("foo", function(){...});
ExternalInterface.call("theFunction", "foo");

But that wouldn't work since theFunction would attempt to do foo(), while it should really do swfObject.foo(). The problem is the page and its Javascript are not under my control (though I can request changes if really needed).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素年丶 2024-10-02 05:18:38

这与 相关问题部分中的第一个问题

按照与该问题的答案相同的思路,您可以执行以下操作:

ExternalInterface.addCallback("foo", function() { /* ... */ });    // The callback
ExternalInterface.call("theFunction(function() { swfObject.foo(); })");

This is closely related to the first question in the related questions section.

Along the same lines as the answer to that question, you can do:

ExternalInterface.addCallback("foo", function() { /* ... */ });    // The callback
ExternalInterface.call("theFunction(function() { swfObject.foo(); })");
迷迭香的记忆 2024-10-02 05:18:38

我认为您误解了文档。此实例中的callback 只是对Flash 内部函数的引用,而不是对您调用的内容的回调。

基本上,您使用 .call() 从 AS 调用 JS 函数;您可以使用 .addCallback() 告诉 Flash Player 应根据名称调用哪个 AS 函数。

在您的示例中,theFunction 将获取一个参数“foo”,这是引用您的匿名 AS 函数的名称。不知道为什么你想要传递这样的函数,但如果你需要,你可以用

function theFunction(callback) {
    // .. do something...
    swfObject[callback]();
}

Now 从 JavaScript 调用它,如果你无法控制 JS/HTML 端,我不确定你是否可以这样做。无论如何,不​​知道为什么你需要 - JS 调用是同步的,就好像它们在同一个线程上运行一样,这意味着 Flash Player 将执行 JS 代码,然后才返回到 Flash Player...你没有等待执行或任何事情。

另外,如果您确实需要在不接触 JS/HTML 端的情况下控制页面,请记住您可以通过 .call 注入整段 JS 代码 - 它不需要是一个简单的函数调用。您可以从 SWF 内部创建整个函数。例如,

var js:XML = <script><![CDATA[
    // Javascript code...
]]></script>;
ExternalInterface.call(js);

或者,如果您需要返回数据,则也不需要回调 - 只需执行一个简单的调用,如

// JS
function isNumberZero(__num) {
    return __num == 0;
}

// AS
trace ("Is number zero = " + ExternalInterface.call("isNumberZero", 10));

不确定这是否有帮助。如果没有,最好有更多关于您到底想做什么的信息。

You're misunderstanding the documentation, I think. callback in this instance is just a reference to a function inside Flash, not a callback to something you call.

Basically, you use .call() to call a JS function from AS; and you use .addCallback() to tell the Flash Player which AS function should be called based on the name.

On your example, theFunction would get one parameter as being 'foo', which is the name that references your anonymous AS function. Not sure why you would want to pass the function like that, but if you need, you could just call it from JavaScript with

function theFunction(callback) {
    // .. do something...
    swfObject[callback]();
}

Now, if you don't have control over the JS/HTML side, I'm not sure if you can do that. Not sure why you'd need, anyway - JS calls are synchronous, as if they were running on the same thread, meaning the Flash Player will execute the JS code and only then return to the Flash Player... you don't have to wait for execution or anything.

Also, if you really need to control the page without touching the JS/HTML side, remember you can inject entire pieces of JS code via .call - it doesn't need to be a simple function call. You can create your entire functions from inside the SWF. For example,

var js:XML = <script><![CDATA[
    // Javascript code...
]]></script>;
ExternalInterface.call(js);

Or, if you need the return data, you don't need a callback either - just do a simple call as in

// JS
function isNumberZero(__num) {
    return __num == 0;
}

// AS
trace ("Is number zero = " + ExternalInterface.call("isNumberZero", 10));

Not sure if this helps at all. If not, it'd be good to have more information on what exactly you're trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文