简体   繁体   English

在IE8中使用JavaScript从Flash中获取变量的问题

[英]Problems with fetching variable from flash with javascript in IE8

I'm creating a quiz and as a timer in the quiz I have a flash movie (an animated clock). 我正在创建一个测验,并且作为测验中的计时器,我有一个Flash电影(一个动画时钟)。 I poll the clock every second to see if the time for the quiz has run out. 我每秒查询一次时钟,以查看测验时间是否用完。

The code for this functionality looks like this: (simplified) 此功能的代码如下所示:(简体)

$(window).load(function() {
    var flashMovie = getFlashMovieObject(flashId);
    var timeElapsed = flashMovie.GetVariable("timeElapsed");
    var timeSet = flashMovie.GetVariable("countdown");
    var degrees = flashMovie.GetVariable("degrees");
    var timerStatus = flashMovie.GetVariable("timerStatus");
});

First of all, it just fetches the flash movie object, and then calls some methods on the object. 首先,它只是获取Flash电影对象,然后在该对象上调用一些方法。 This works fine in Firefox (pc & mac), Safari (mac) but in IE8 on pc it returns 'unexpected error on line 3' (or any other line that uses the flashMovie object). 在Firefox(PC和Mac),Safari(Mac)中运行良好,但在PC上的IE8中,它会返回“第3行出现意外错误”(或其他任何使用flashMovie对象的行)。

The code for the getFlashMovieObject() function looks like this: getFlashMovieObject()函数的代码如下所示:

function getFlashMovieObject(movieName)
{    
    if (navigator.appName.indexOf ("Microsoft") !=-1) {
        return window[movieName];
    }

    return document[movieName];
}

Any help is appreciated! 任何帮助表示赞赏!

UPDATE: I have figured out that if set IE8 clear the cache for each reload, then this occurs. 更新:我已经发现,如果将IE8设置为每次重新加载都清除缓存,则会发生这种情况。 If I don't, then it only fails the first time, and all subsequent reloads work fine. 如果我不这样做,那么它只会在第一次失败,并且随后的所有重新加载都可以正常工作。 I don't understand how the cache can resolve this issue. 我不了解缓存如何解决此问题。

In my experience, the getFlashMovieObject function has some quirks in some browsers, and I found several versions of it. 以我的经验,getFlashMovieObject函数在某些浏览器中有一些古怪之处,我发现了它的多个版本。 This one seems to fair pretty well in most browsers: 在大多数浏览器中,这似乎很不错:

function getFlashMovieObject(movieName){
  if(document.embeds[movieName])
    return document.embeds[movieName];
  if(window.document[movieName])
    return window.document[movieName];
  if(window[movieName])
    return window[movieName];
  if(document[movieName])
    return document[movieName];
  return null;
}

If you happen to use jQuery, I found that something as simple as 如果您碰巧使用jQuery,我发现简单到

$("#flashobject")[0].GetVariable("timeElapsed");

seems to work on every browser. 似乎适用于所有浏览器。

At a wild guess, IE is incorrectly firing the window.load event too early, before your swf is fully loaded. 令人吃惊的是,IE在完全加载SWF之前错误地触发window.load事件。 That might explain why clearing your cache forces the error. 这也许可以解释为什么清除缓存会导致错误。

If you're not already doing so, use SWFObject to embed your Flash, then you can use the loaded callback to know when your Flash object is really available. 如果尚未这样做,请使用SWFObject嵌入Flash,然后可以使用已加载的回调来知道Flash对象何时真正可用。

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

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