简体   繁体   中英

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). 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. 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).

The code for the getFlashMovieObject() function looks like this:

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. 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. 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

$("#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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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