home


Plan A - lets just make a big URL string...

There is no JavaScript being used here.. just sending text in via an URL parameter in the HTML used to embed the SWF in this page. It looks like this:
<param name="movie" value="main.swf?theString=123456789a123456789b123456789c123456789d123456 ... etc "

This is NOT the best way to send big strings into a SWF. Here's why.. if you view this page with Internet Explorer (as of ver 6) there's a bug in IE that only allows 4048 characters (at least that's what I'm counting..) in the string including the name of the SWF and the variable name - i.e. the entire URL must be less than that or the SWF won't load in IE. If you have a long SWF name and several long variable names in that URL then it will eat into the length of the string you can use. Also remember that strings have to be URLencoded before use, so some characters will expand to multiple characters ( a "space" character becomes %20).

With Firefox/Mozilla I'm not seeing that URL limit.. I can send 140K+ character strings into the SWF in an URL string with no problem. But... it's just asking for trouble. It's just plain ugly. Don't do this..



So here's plan B..

a new parameter was introduced with Flash Player 6. You can use the FlashVars parameter in the HTML object/embed tags. I won't go into details because there's a nice technote on how to use FlashVars. The thing to take away is this: "All browsers will support string sizes of up to 64KB (65535 bytes) in length" That's a good improvement if you're targeting IE browsers..

Using URL strings or Flashvar is only good for passing data in that exists before the SWF loads. You can use PHP or CF or some other means to dynamically generate your HTML page and have that data essentially "hardcoded" into the HTML. This removes the requirement of using JavaScript to send the data into the SWF. This might be good if you have a requirement that no JS be used.. So if you already have the data all packaged up and ready to send into the SWF before the HTML page is rendered and the SWF is loaded then you now have a couple of luke warm options.


And here's plan C..

Click the button below to send a string into the SWF using the JavaScript/Flash setVariable method.. I was able to send a string with 500,000 characters into Firefox and IE. On my system (which is probably nearing "average speed".. sigh..) it takes less than a second. So getting really big strings into Flash via JavaScript is technically possible.. but we can do it faster.. scroll on down....


Now.. if you break that big string up a bit.. say down to 10,000 character chunks and send them into the SWF.. then things get interesting. Here's an example sending 2 Million characters into a SWF.. on my ancient 2Ghz system this takes less than 250ms.. This isn't a straightforward process though, like the ones mentioned above. Instead of just setting a variable in the SWF to some jumbo sized string we have to break it up into little pieces and then reassemble it in the SWF. So there's a little bit of code in the SWF that pushes each incoming string fragment into an Array. When the JS sends the last string it sets a variable to indicate it's finished and the SWF then joins the pieces in the array back into one big string. Ideally you'll also have something in the SWF that validates the length of the string to make sure everything went as planned. But.. hopefully you won't need to send a 2Mb string into a SWF..



And here's plan D..

Use URLstrings, FlashVar, or setVariable only if you have to get the data into the SWF via the browser (the host HTML page) rather than getting it from the server via Flash Remoting, AMFPHP, or some other standard method. If your SWF can call the server and request the data then that opens up other routes for getting data in and out. But.. there are some situations where you're forced to send the data into and out of the SWF via JavaScript.. or where it just makes better sense to have that data packaged into the HTML page rather than having to make another call to the server for it.

I just thought I'd mention that Flash can make calls to the server, lest somebody come here and think that the only way to get data into a SWF is by these methods.. that would suck wouldn't it?


And here's plan E..

Also.. if you are able to target the Flash Player 8 then you can use the externalInterface class..but some have found externalInterface to be slower at moving data between Flash and JavaScript.. I'm still delivering to Flash 7, so haven't done any testing.. please shoot me an e-mail if you have input on whether externalInterface really is as bad as Brad says it is..

download the FLAs (not much in them.. but if you want them.. )