SWFObject Error passing URI’s and HTML code into flashvars
When passing URL's or HTML code through SWFObject flashvars, some characters cause errors.
Error type: Pitfall
Error class: -
How to Induce error:
Flash file, published as 'example.swf', 550 x 400:
//in timeline import flash.text.TextField; import flash.display.LoaderInfo; //this is for Flashvars var paramObj:Object = LoaderInfo(root.loaderInfo).parameters; var myTextField:TextField = new TextField(); myTextField.width = 400; if(paramObj['testVar']){ trace('found flashvar'); myTextField.text = paramObj['testVar']; }else{ trace('no flashvar found'); myTextField.text = 'no flashvar found'; } addChild(myTextField); stop();
HTML:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>swfObject demo</title> <script type="text/javascript" src="swfobject.js"></script> </head> <body bgcolor="#ffffff"> <div id="flashcontent"> This text is replaced by the Flash movie. </div> <script type="text/javascript"> var so = new SWFObject("swfObject_flashvar_error.swf", "swfObject_flashvar_error", "550", "400", "8", "#ffffff"); so.addVariable("testVar", "Lots & lots & lots & lots of ampersands"); so.write("flashcontent"); </script> </body> </html>
Ensure that swfObject.js is in the correct location (same folder as the .swf and .html in the above example).
If you are getting the 'no flashvar found' message, make sure you are not accessing the .html through your file system (you will know this if the start of your URL is 'file://' or something similar).
We expect to see the words 'Lots & lots & lots & lots of ampersands.' in the Flash, but if you have the correct file structure on a live environment, you'll find that you can only see the word 'Lots', this is because SWFObject doesn't like the '&' character.
This poses a huge issue with words encoded in different languages, and URL's which typically require several non-standard characters
Solution:
Simply use the encodeURIComponent() function in the HTML on your flashvar values:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>swfObject demo</title> <script type="text/javascript" src="swfobject.js"></script> </head> <body bgcolor="#ffffff"> <div id="flashcontent"> This text is replaced by the Flash movie. </div> <script type="text/javascript"> var so = new SWFObject("swfObject_flashvar_error.swf", "swfObject_flashvar_error", "550", "400", "8", "#ffffff"); so.addVariable("testVar", encodeURIComponent("Lots & lots & lots & lots of ampersands")); so.write("flashcontent"); </script> </body> </html>
The encodeURIComponent is available for all Mozilla-style browsers, Safari, Opera, Firefox, Chrome and IE versions 5.5 and above. For more information on this workaround, visit the SWFObject FAQ.
Categories
- 300 Spartan Tips in Flash (10)
- Blog (58)
- Corona (1)
- Flash (40)
- Flash Errors (7)
- Flash Quest (17)
- Flash Quest – Prologue (17)
- Flex (5)
- Game Design (10)
- Theory (10)
- Uncategorized (2)
February 19th, 2010 - 14:19
Thanks for supplying the URL!
I’ve also been watching this discussion, and there are some developers who have reason to believe that it is not a bug at all. Until it gets resolved, I’m confident that my workaround will serve its purpose.
Those who are anxious that a future fix might affect their functionality can also use AC_RunActiveContent.js as an alternative to SWFObject.js
February 19th, 2010 - 01:22
Note that this is (arguably) a bug in swfobject, which is reported here:
http://code.google.com/p/swfobject/issues/detail?id=66