I’ve added a swfobject helper to the Ruboss plugin that does a few things for you:
1) It adds a cache-breaking time stamp to the .swf’s url so that you and your users don’t have to refresh their caches when your .swf changes.
2) It allows you to pass Ruby hashes for the flashVars, params and attributes parameters.
3) It will automagically create the flashContent div for you if you want
Here’s what a basic call would look like:
<html>
<head>
<title>some page</title>
<%= javascript_include_tag '/bin/swfobject' %>
</head>
<body>
<%= swfobject('/bin/YourSwf.swf') %>
<div id='flashContent'></div>
</body>
</html>
If you’re feeling extra lazy and want to have the flashContent div created automatically, then add a :create_div => true parameter to the swfobject call:
<html>
<head>
<title>some page</title>
<%= javascript_include_tag '/bin/swfobject' %>
</head>
<body>
<%= swfobject('/bin/YourSwf.swf', :create_div => true) %>
</body>
</html>
Only the first argument, the path to the .swf object, is required. The options and their defaults are:
:width => '100%',
:height => '100%',
:id => 'flashContent',
:version => nil,
:express_install_swf => nil,
:flash_vars => nil,
:params => nil,
:attributes => nil,
:create_div => false
A fully blown call to swfobject might look like this:
<%= swfobject('/bin/YourSwf.swf',
:flash_div => 'flashContent',
:width => '100%',
:height => '100%',
:version => '9.0.0',
:express_install_swf => '/bin/expressInstall.swf',
:flash_vars => 'flashVars',
:create_div => true
) %>
Most of the options are pretty self-explanatory, but here’s some info on a couple of them:
:id gives the id of the div that the flash object will be created in. If :create_div is false, you must create the div yourself. If :create_div is true, it will be created for you.
:flash_vars, :options and :attributes can be passed in as either a Hash or a String. As a Hash they are translated to a Javascript Hash. As a String, they are assumed to represent the name of an existing Javascript variable. Here’s an example of passing in a Hash to flash_vars:
<html>
<head>
<title>some page</title>
<%= javascript_include_tag '/bin/swfobject' %>
</head>
<body>
<%= swfobject('/bin/YourSwf.swf', :create_div => true,
:flash_vars => {'user_id' => @user.id, 'user_name' => @user.name}) %>
</body>
</html>
Here’s an example of passing in the name of a Javascript variable:
<html>
<head>
<title>some page</title>
<%= javascript_include_tag '/bin/swfobject' %>
</head>
<body>
<script>
// Get the user information somehow, then add it to flashVars
var flashVars = {user_id: user.id, user_name: user.name};
</script>
<%= swfobject('/bin/YourSwf.swf', :create_div => true, :flash_vars => 'flashVars') %>
</body>
</html>
Being able to pass in a name can come in handy if you want to pass in variables from the URL.
<html>
<head>
<title>some page</title>
<%= javascript_include_tag '/bin/swfobject' %>
</head>
<body>
<script>
//Grab the flashVars from the URL search string.
//This is not a perfect implementation: it will break, for example, if
//you have an anchor tag, and it hasn't been tested with
//URL encoded strings.
var searchString = unescape(document.location.search);
var flashVars = {};
// strip off the leading '?' and split by '&'
var nvPairs = searchString.replace(/^\?/, '').split("&");
for (i = 0; i < nvPairs.length; i++)
{
var nvPair = nvPairs[i].split(”=”);
var name = nvPair[0];
var value = nvPair[1];
flashVars[name] = value
}
</script>
<%= swfobject(’/bin/YourSwf.swf’, :create_div => true, :flash_vars => ‘flashVars’) %>
</body>
</html>
That’s it. To get this, update your plugin to the latest version.
Scott Patten