View Full Version : Flash Object Creator

08-30-2009, 05:13 AM
function flashObject(src, id, size, params) {
if(!src || !id || !size || !params)
return false;
// We have to do different stuff for IE since it's so pathetically horrible and not standards-compliant.
var is_ie = navigator.appName == 'Microsoft Internet Explorer' ? true : false;
var flashObject = document.createElement(is_ie ? 'embed' : 'object');
flashObject.setAttribute('id', id);
flashObject.style.width = size[0];
flashObject.style.height = size[1];
flashObject.setAttribute('type', 'application/x-shockwave-flash');

if(is_ie) {
flashObject.setAttribute('pluginspage', 'http://www.macromedia.com/go/getflashplayer');
flashObject.setAttribute('src', src);
} else {
flashObject.setAttribute('data', src);

for(i = 0; i <= params.length - 1; i++) {
if(is_ie) {
flashObject.setAttribute(params[i][0], params[i][1]);
} else {
var param = document.createElement('param');
param.setAttribute('name', params[i][0]);
param.setAttribute('value', params[i][1]);
return flashObject;

What this pretty little piece of coding does is create a flash object with the ID, source, size (array - [width, height]), and parameters (array - [['paramName', 'paramValue'], ['name2', 'value2']]) that you specify.

Example usage:

<span id="example">Lol.</span>
<script type="text/javascript" src="flashObject.js"></script>
<script type="text/javascript">// <![CDATA[
exampleObject = flashObject('http://www.example.com/flash.swf', 'exampleObject', [200, 500], [['flashvars', 'lol&lol2=roflcakes'], ['allowFullScreen', 'true']]);
// ]]>

I made it as a replacement to the way I was using to create my Chatango chat object in another one of my scripts, but this works well with any flash object in general. :D