Detect Flash with JavaScript

June 5, 2013

Quick, easy and lightweight way to detecting flash support in clients.


Recently I had to find a a good way of detecting if Flash is enabled in the browser, there are the two main libraries Adobe Flash Detection Kit and SWFObject which are both very good at detecting whether Flash is enabled as well as getting the version of Flash installed and useful for dynamically embedding and manipulating swf files in your web application. But all I needed was a yes or a no to whether Flash was there or not without the added overhead of unneeded code. My goal was to wrote the least amount of JavaScript while still being able to detect cross browser for Flash.

function detectflash(){
    if (navigator.plugins != null && navigator.plugins.length > 0){
        return navigator.plugins["Shockwave Flash"] && true;
    }
    if(~navigator.userAgent.toLowerCase().indexOf("webtv")){
        return true;
    }
    if(~navigator.appVersion.indexOf("MSIE") && !~navigator.userAgent.indexOf("Opera")){
        try{
            return new ActiveXObject("ShockwaveFlash.ShockwaveFlash") && true;
        } catch(e){}
    }
    return false;
}

For those unfamiliar with the tilde (~) operator in javascript, please read this article, but the short version is, used with indexOf these two lines are equivalent:

~navigator.appVersion.indexOf("MSIE")
navigator.appVersion.indexOf("MSIE") != -1

To use the above function:

if(detectflash()){
    alert("Flash is enabled");
} else{
    alert("Flash is not available");
}

And that is it. Pretty simple and a much shorter version that the alternatives, compressed and mangled I have gotten this code to under 400 Bytes. I tested this code with IE 5.5+, Firefox and Chrome without any issues.

comments powered by Disqus