简体   繁体   English

从js问题调用As函数

[英]Calling As function from js problem

I have a swf file that is not controlled by me. 我有不受我控制的swf文件。 The swf expects a javascript call to set some variables after initialization. SWF希望在初始化后通过JavaScript调用来设置一些变量。

The swf is embedded using the swfobject and I'm trying to call the as function right after the embed. swf是使用swfobject嵌入的,并且我尝试在嵌入后立即调用as函数。 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. 其他一切都应该没问题,因为通过Firebug手动调用as函数不会产生错误。

So the question is how do I call the function when the embed is complete? 所以问题是嵌入完成后如何调用该函数?

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. 您可以通过在Flash准备好后让Flash回调javascript来做到这一点,但是您说swf不在您的控制范围内。 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. 我真正想到的就是使用onload方法来确保页面已完成加载,然后在尝试使用它之前插入一小段延迟。 Look at the setTimeout javascript function for that. 查看setTimeout javascript函数。 Not a great solution though. 虽然不是一个很好的解决方案。

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. 如果确实如此,那么您可以将其包装在while循环中。 A bit less nasty than a setTimeOut! 比setTimeOut少一些讨厌!

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. 将变量作为flashvars传入。 They will be available to the flash movie immediately. 它们将立即可用于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. 加载Flash后,它应调出您的页面,通常有一个合同定义了可以/必须实现的Flash影片调用方法。 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文件后将调用MovieLoaded() ,然后可以将依赖于正在加载的电影的任何脚本放入此方法中...

     function MovieLoaded() { doSomething(); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM