"Never memorize something you can look up", Albert Einstein

SWFObject 1 and 2 compatible code

At our work we frequently have to work with deprecated code and frameworks, today I was confronted with the problem that I got code that has to run on a website with the latest SWFObject (2) and an older version (1), you probably know that both versions have another syntax for adding a SWF-object to the webpage.

Here is my small (and not perfect) solution:

	/**
	 * Function that is compatible with the old 'new SWFObject()' and new 'swfobject.embedSWF'
	 * @return success
	 */
	function swfObjectCreate(swfPath, idName, width, height, className, path, settingFile, chartData, container)
	{
		log("swfObjectCreate() call");
		container.attr("id", idName);
	
		if(typeof SWFObject == "function")
		{
			var so = new SWFObject(swfPath, idName, width, height, "8", "#FFFFFF");
			so.setAttribute("class", className);
			so.addVariable("path", path);
			so.addVariable("settings_file", settingFile);
	
			so.addVariable("chart_data", encodeURIComponent(chartData));
			so.write(idName);		
		}
		else if(typeof swfobject == "object")
		{
	        var flashvars = {
	                "path" : path,
	                "settings_file" : settingFile,
	                "chart_data" : encodeURIComponent(chartData)
	        };
	
	        swfobject.embedSWF(swfPath, idName, width, height, "9.0.0",
	        		EXPRESS_INSTALL_PATH, flashvars,
	                {}, {"class":className}
	        );		
		}
		else
		{
			log("No SWFObject found in Javascript");
			return false;
		}
	
		return true;
	}

	function log(msg)
	{
		if(typeof console == "object")
		{
			console.log(msg);
		}
	}

 


Only members are allowed to post a comment.

Comments

No comments have been placed.