views:

535

answers:

2

I'm using swfObject to embed a flash player in my app. Sporadically, I see errors in FireFox (only, not in IE or Chrome) when trying to communicate with the embedded flash object. The error says "SetVariable is not a function". The code which causes this error executed fine just moments ago, and now shows this error. If I re-load the page and re-run, odds are decent that this doesn't happen again.

I've seen reports on the web about FF not working well with SetVariable, but nothing to fix it. Apparently swfObject should hide all of this but it doesn't.

Here's what my code looks like:

...
var flashvars=...
var params=...
var attributes = {};
attributes.id = "my_player";

if( swfobject.hasFlashPlayerVersion("9.0.0") )
{
    swfobject.embedSWF("my_player_js.swf", "my_player_holder", "1", "1", "9.0.0", "", flashvars, params, attributes );
}
....

document.getElementById("my_player").SetVariable( "method:stop", "");

That last line, on FF, sometimes causes the "SetVariable is not a function" error.

Any suggestions on where to look?

A: 

I don't know swfObject or Flash, but errors similar to these often happen if you run code on an object before it has been initialised. What triggers your code to run? onload? DOMContentLoaded?

Matthew Wilson
+1  A: 

You should use ExternalInterface instead, it's much more robust than SetVariable (slower, but unless you really need the speed, you should use it)

One common case that usually causes this is trying to call the SetVariable call before the swf is loaded. Generally it's safer to have your swf call out to the page and tell the js that it's ready to receive calls before you try and call methods.

Since you say that the 'The code which causes this error executed fine just moments ago' it makes me think that the above may not be your issue, but it is worth checking anyway. Perhaps if you are modifying the swf on the page, the browser is trying to reload it, and that causes it to go away for a few milliseconds at a time?

Geoff