Detecting SWFObject Version

Some people are in the curious position of needing to programmatically check to see which version of SWFObject is available (if any). The easiest way to do it is to check for the existence of the SWFObject JavaScript object:

function getSwfObjectVersion(){	
    var version = (window.swfobject) ? "2" : (window.SWFObject) ? "1" : false;
    if(version === "2"){
            version = "2.2";
        } else if(swfobject.removeSWF){
            version = "2.1";
        } else {
            version = "2.0";	
    return version;

This function works by checking the global window object for the existence of objects named swfobject and SWFObject. There are four things going on here:

1. The code checks the window object for the swfobject variable, which allows us to skip checking for null values (swfobject !== null). Browsers won't throw a "swfobject is undefined" error if swfobject isn't found.

2. Since JavaScript is case-sensitive, two objects with the same name but different case will appear as two unique objects. SWFObject 1.x was written with uppercase SWFO, while SWFObject 2.x is written with all lowercase letters.

3. In the event both versions are found, the function returns SWFObject 2, which is the preferred version.

4. Feature detection is used to determine which version of SWFObject is being loaded.


Questions or Comments?

If you have questions or would like to point out an error, please post your remarks in the SWFObject Google Group. Thanks!