从js问题调用As函数

时间:2021-01-14 15:58:30

I have a swf file that is not controlled by me. The swf expects a javascript call to set some variables after initialization.

我有一个不受我控制的swf文件。 swf期望javascript调用在初始化之后设置一些变量。

The swf is embedded using the swfobject and I'm trying to call the as function right after the embed. This appears to be too soon because I get an error. Everything else should be fine since calling the as function manually via firebug does not produce the error.

swf是使用swfobject嵌入的,我试图在嵌入后立即调用as函数。这似乎太快了,因为我收到了错误。其他一切都应该没问题,因为通过firebug手动调用as函数不会产生错误。

So the question is how do I call the function when the embed is complete?

所以问题是如何在嵌入完成后调用该函数?

3 个解决方案

#1


1  

Are you doing this while the page is still loading? Or from am onload handler? If it's inline javascript I would suggest doing it in the onload handler from javascript which you can do like this -

你还在加载页面的时候这样做吗?或者从上传处理程序?如果它是内联javascript我建议在javascript的onload处理程序中执行它,你可以这样做 -

window.onload = function() {
  // your code here 
}

it will run your code once the page is fully loaded.

一旦页面完全加载,它将运行您的代码。

This doesn't guarentee that the flash is initialised though. You could do that by having the flash make a callback to javascript once it is ready, but you said that the swf is not in your control. All I can really think of us using the onload method to make sure the page is finished loading, and then insert a short delay before trying to use it. Look at the setTimeout javascript function for that. Not a great solution though.

这并不保证闪存已初始化。你可以通过让flash在准备就绪后回调javascript来做到这一点,但你说swf不在你的控制范围内。我真的可以想到我们使用onload方法来确保页面加载完毕,然后在尝试使用它之前插入一个短暂的延迟。看看setTimeout javascript函数。虽然不是很好的解决方案。

#2


0  

I found some code for checking whether the function exists yet. In summary:

我找到了一些用于检查函数是否存在的代码。综上所述:

if (typeof yourFunctionName == 'function') {
    yourFunctionName();
}

Does that work for you? If it does then you can just wrap in a while loop. A bit less nasty than a setTimeOut!

那对你有用吗?如果确实如此,那么你可以将while循环包裹起来。比setTimeOut稍微讨厌一点!

#3


0  

When integrating Flash and HTML / JavaScript, there are several common approaches, which have been designed to eliminate this problem.

在集成Flash和HTML / JavaScript时,有几种常见的方法可以解决这个问题。

  1. Pass in the variables as flashvars. They will be available to the flash movie immediately.

    将变量作为flashvars传递。它们将立即用于flash电影。

  2. When the flash has loaded, it should call out to your page, normally there is a contract that defines the methods you can / must implement for the flash movie to call. For example, it would state that it will call MovieLoaded() when the flash file has loaded, and you could then put any scripts dependent on the movie being loaded within this method...

    当闪存加载时,它应该调出你的页面,通常有一个合同,定义你可以/必须实现的方法来调用flash电影。例如,它会声明它将在加载Flash文件时调用MovieLoaded(),然后您可以根据此方法中加载的影片放置任何脚本...

    function MovieLoaded() {
        doSomething();
    }
    

#1


1  

Are you doing this while the page is still loading? Or from am onload handler? If it's inline javascript I would suggest doing it in the onload handler from javascript which you can do like this -

你还在加载页面的时候这样做吗?或者从上传处理程序?如果它是内联javascript我建议在javascript的onload处理程序中执行它,你可以这样做 -

window.onload = function() {
  // your code here 
}

it will run your code once the page is fully loaded.

一旦页面完全加载,它将运行您的代码。

This doesn't guarentee that the flash is initialised though. You could do that by having the flash make a callback to javascript once it is ready, but you said that the swf is not in your control. All I can really think of us using the onload method to make sure the page is finished loading, and then insert a short delay before trying to use it. Look at the setTimeout javascript function for that. Not a great solution though.

这并不保证闪存已初始化。你可以通过让flash在准备就绪后回调javascript来做到这一点,但你说swf不在你的控制范围内。我真的可以想到我们使用onload方法来确保页面加载完毕,然后在尝试使用它之前插入一个短暂的延迟。看看setTimeout javascript函数。虽然不是很好的解决方案。

#2


0  

I found some code for checking whether the function exists yet. In summary:

我找到了一些用于检查函数是否存在的代码。综上所述:

if (typeof yourFunctionName == 'function') {
    yourFunctionName();
}

Does that work for you? If it does then you can just wrap in a while loop. A bit less nasty than a setTimeOut!

那对你有用吗?如果确实如此,那么你可以将while循环包裹起来。比setTimeOut稍微讨厌一点!

#3


0  

When integrating Flash and HTML / JavaScript, there are several common approaches, which have been designed to eliminate this problem.

在集成Flash和HTML / JavaScript时,有几种常见的方法可以解决这个问题。

  1. Pass in the variables as flashvars. They will be available to the flash movie immediately.

    将变量作为flashvars传递。它们将立即用于flash电影。

  2. When the flash has loaded, it should call out to your page, normally there is a contract that defines the methods you can / must implement for the flash movie to call. For example, it would state that it will call MovieLoaded() when the flash file has loaded, and you could then put any scripts dependent on the movie being loaded within this method...

    当闪存加载时,它应该调出你的页面,通常有一个合同,定义你可以/必须实现的方法来调用flash电影。例如,它会声明它将在加载Flash文件时调用MovieLoaded(),然后您可以根据此方法中加载的影片放置任何脚本...

    function MovieLoaded() {
        doSomething();
    }