Friday, October 12, 2007

flashVars empty after Internet Explorer fix

In a previous post I was writing about a fix to remove the Press Spacebar or Enter to activate or use this control message. But it also made a mess out of a flash file I was trying to use. After a lot of looking into the source code (Flex) and trying to understand what happened, I found a solution.

First of all, you will find a lot of web pages advocating using a parameter called flashVars for sending parameters to Flash applications. Then you would use Application.application.parameters to get the variables URLEncoded in flashVars. That works in most cases, but not when:
a. you used an IE fix for the annoying message
b. you have in the movie and/or src parameters an url that has parameters. (like when you would get the SWF file out from an HttpHandler, for example)

The solution is to put all the variables you would put in the flashVars in the url of the movie or src parameters.

So, instead of :
<param name='movie' value='test.swf' />
<param name='flashVars' value='a=%27test%27&b=12' />

<param name='movie' value='test.swf?a=%27test%27&b=12' />

Don't forget to UrlEncode the values.

But why does this happen? Apparently, the problem is that after setting the innerHTML property the FlashVars param element has an empty value. I tried to fix the javascript to 1. get the value of FlashVars 2. do the fix 3. replace the empty FlashVars value with the saved value. It did not work! Even by javascript, after finding the param element with the name of FlashVars and an empty value, I could not set the value of the element. It probably has to do with the IE "fix". Something must clear the FlashVars when being set from anything else but from source.


Tudor said...

This is beyond stupid! Why don't you use SWFObject, like everybody else?

Siderite said...

Because SWFObject, as lovely and Javascript guru as it sounds, does the exact same thing. It creates html then fills some innerHTML property.

What would be beyond stupid would be to recreate the HTML in Javascript when I already have it from my codebehind.

Anonymous said...

Don't listen to the tudor idiot. This code just made my day a little brighter!

Solving these freak situations in IE is a headache. Thank you for your help!

Anonymous said...

totally agreed...swfobject can be cumbersome to work with when this little gem helps out greatly

nithon said...

just wanted to say thanks that was just what i was lookig for :)

Anonymous said...

You rocks !! i search it about 4 hours. You saved my life. Thanks a lot.[Fuck IE]

Anonymous said...

this is simply saves my day...thanks a lot

Bruce Jawn said...

Thank you for that!

Anonymous said...

To those suggesting SWFObject, great as that is, there are situations where it can't reach - for example if you need to save the contents of a DIV via innerHTML, or if you're duplicating an HTML fragment where the contents are unknown.

Elar said...

Thanx a lot. The IE fix was only way to restart hidden flash playing from beginning after setting it visible on IE 7 & 8. Saved me a great headache to find solution for flashvars problem :)

Bruno said...

Thaks for your solution. Very simple and very efficient. Helped me a lot

Antoine said...

Thank you very much =D