Cross Browser Flash Detection in Javascript

105,163

Solution 1

SWFObject is very reliable. I have used it without trouble for quite a while.

Solution 2

I agree with Max Stewart. SWFObject is the way to go. I'd like to supplement his answer with a code example. This ought to to get you started:

Make sure you have included the swfobject.js file (get it here):

<script type="text/javascript" src="swfobject.js"></script>

Then use it like so:

if(swfobject.hasFlashPlayerVersion("9.0.115"))
{
    alert("You have the minimum required flash version (or newer)");
}
else
{
    alert("You do not have the minimum required flash version");
}

Replace "9.0.115" with whatever minimum flash version you need. I chose 9.0.115 as an example because that's the version that added h.264 support.

If the visitor does not have flash, it will report a flash version of "0.0.0", so if you just want to know if they have flash at all, use:

if(swfobject.hasFlashPlayerVersion("1"))
{
    alert("You have flash!");
}
else
{
    alert("You do not flash :-(");
}

Solution 3

I know this is an old post, but I've been looking for a while and didn't find anything.
I've implemented the JavaScript Flash Detection Library. It works very well and it is documented for quick use. It literally took me 2 minutes. Here is the code I wrote in the header:

<script src="Scripts/flash_detect.js"></script>
<script type="text/javascript"> 
 if (!FlashDetect.installed) {
    alert("Flash is required to enjoy this site.");         
 } else {
    alert("Flash is installed on your Web browser.");
 }
</script>        

Solution 4

You could use closure compiler to generate a small, cross-browser flash detection:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @formatting pretty_print
// @use_closure_library true
// ==/ClosureCompiler==

// ADD YOUR CODE HERE
goog.require('goog.userAgent.flash');
if (goog.userAgent.flash.HAS_FLASH) {
    alert('flash version: '+goog.userAgent.flash.VERSION);
}else{
    alert('no flash found');
}

which results in the following "compiled" code:

var a = !1,
    b = "";

function c(d) {
    d = d.match(/[\d]+/g);
    d.length = 3;
    return d.join(".")
}
if (navigator.plugins && navigator.plugins.length) {
    var e = navigator.plugins["Shockwave Flash"];
    e && (a = !0, e.description && (b = c(e.description)));
    navigator.plugins["Shockwave Flash 2.0"] && (a = !0, b = "2.0.0.11")
} else {
    if (navigator.mimeTypes && navigator.mimeTypes.length) {
        var f = navigator.mimeTypes["application/x-shockwave-flash"];
        (a = f && f.enabledPlugin) && (b = c(f.enabledPlugin.description))
    } else {
        try {
            var g = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7"),
                a = !0,
                b = c(g.GetVariable("$version"))
        } catch (h) {
            try {
                g = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6"), a = !0, b = "6.0.21"
            } catch (i) {
                try {
                    g = new ActiveXObject("ShockwaveFlash.ShockwaveFlash"), a = !0, b = c(g.GetVariable("$version"))
                } catch (j) {}
            }
        }
    }
}
var k = b;
a ? alert("flash version: " + k) : alert("no flash found");

Solution 5

Minimum version I've ever used (doesn't check version, just Flash Plugin):

var hasFlash = function() {
    return (typeof navigator.plugins == "undefined" || navigator.plugins.length == 0) ? !!(new ActiveXObject("ShockwaveFlash.ShockwaveFlash")) : navigator.plugins["Shockwave Flash"];
};
Share:
105,163
Ta01
Author by

Ta01

I believe that our kind is cursed. We are cursed to lose our greatest warriors; our most noble heroes; our most gifted scholars.

Updated on April 04, 2020

Comments

  • Ta01
    Ta01 about 4 years

    Does anyone have an example of script that can work reliably well across IE/Firefox to detect if the browser is capable of displaying embedded flash content. I say reliably because I know its not possible 100% of the time.

  • davr
    davr over 15 years
    Same here, SWFObject works great for me as well (used to be called FlashObject, but Adobe threw a hissy fit)
  • Andrew Ensley
    Andrew Ensley over 13 years
    Would just like to point out: SWFObject is the successor to Adobe's detection kit linked here.
  • Brian Scott
    Brian Scott over 13 years
    great. I was struggling to find a really simple example of simply detecting any flash installed. Thanks.
  • balint
    balint over 12 years
    it now fails on Chrome and Firefox 6+!
  • kontur
    kontur over 12 years
    Thanks for this example! Needed to run some other javascript if the user was without flash and was already using swfobject for the embedding anyway. :)
  • anonymous-one
    anonymous-one about 12 years
    this solution is the cleanest in our opinion. we were looking for a swfobject / library free method of detecting if flash is installed. this does the trick. thanks!
  • Jon z
    Jon z almost 12 years
    With jQuery and swfobject, this is the code I used to add Modernizr-style html class names: $('html').addClass(typeof swfobject !== 'undefined' && swfobject.getFlashPlayerVersion().major !== 0 ? 'flash' : 'no-flash');
  • Arindam Paul
    Arindam Paul over 11 years
    Awesome solution :) .. you saved my day.
  • hitautodestruct
    hitautodestruct over 11 years
    Can you provide some code or at least a link as to how you achieved the answer on your site?
  • Ates Goral
    Ates Goral over 11 years
    @hitautodestruct If you view the source of the page, the answer is directly there, at the top of the JS block.
  • hitautodestruct
    hitautodestruct over 11 years
    So essentially were talking about this code (jsbin link)?
  • Ates Goral
    Ates Goral over 11 years
    @hitautodestruct Yes, plus the detectObject() counterpart for IE.
  • hitautodestruct
    hitautodestruct over 11 years
    If anyone is interested I tested some of these cases on jsperf. SWFObject came out the fastest.
  • mike nelson
    mike nelson over 11 years
    Nice and short, I like it!
  • poitroae
    poitroae over 11 years
    Seems legit - the latest version works pretty well or me. Thank you, keep up the good work!
  • Tom Roggero
    Tom Roggero over 11 years
    @greg.kindel I've been using it for a long time since then, and it works for the versions I tested. Could be helpful though, if you specify OS version and IE version too ;)
  • Zensursula
    Zensursula about 11 years
    doesn't work in IE 9.0.17 under Win 7 with error message: "Automation server can't create object". This seems to depend on the installation. On some computers it works on some not.
  • Zensursula
    Zensursula about 11 years
    @Zensursula: I put a try{}catch() around the ActiveXObject and returned false in the exception clause. Now It is working for mee too
  • Andrew Ensley
    Andrew Ensley almost 11 years
    Not to complain @anonymous-one, but doesn't this solution also use a library (specifically goog.userAgent.flash from Google's Closure Compiler)? I just want to make sure I'm not missing some nuanced difference here.
  • anonymous-one
    anonymous-one almost 11 years
    we are not using the first snippet. we are using the 2nd. which is 'library free'.
  • HartleySan
    HartleySan over 10 years
    This is the purest and most thorough answer I've seen that seems to cover all browsers. Thank you.
  • James M. Greene
    James M. Greene over 10 years
    You can also see the unminified version of this compiled code, including some helpful comments, over here: code.google.com/p/doctype-mirror/wiki/ArticleDetectFlash
  • DaEagle
    DaEagle over 10 years
    Thanks. I like this library because it doesn't require a sentinel swf. I hope you keep it up to date!
  • Avatar
    Avatar about 10 years
    Check for flash using swfobject with if( swfobject.hasFlashPlayerVersion("8.0") ) { } Throws false if there is no flash installed. The number is the minimum flash player version required.
  • Akrikos
    Akrikos almost 10 years
    hitautodestruct, of course SWFObject came out fastest. It only does the actual detection once on page load and then returns the values it stored every time it's called. Since that's the way you'd end up using the other methods as well, the performance comparison isn't a fair comparison.
  • mch
    mch about 9 years
    This can create a error in IE depending on its security settings.
  • Tom Roggero
    Tom Roggero about 9 years
    @mch a ton of things could do that. but the by default security levels should not.
  • mch
    mch about 9 years
    No, not the default ones, you are right. The error popped up in IE8 on Windows 7, with network configured as "Work environment" (not home network)
  • E-comm
    E-comm about 9 years
    there is a problem with this, you will need to include a check for SWFobject or you will get a error because undefined does not have a function called hasFlashPlayerVersion(). if(SWFobject && SWFobject.hasFlashPlayerVersion("1")) { // code here }
  • Andrew Ensley
    Andrew Ensley about 9 years
    Obviously, my code assumes that you have SWFObject loaded. It's just like using jQuery or any other library for a solution. It won't work if you don't include it, and it would be a lot of extra bloat/execution logic if you checked for the library every time you used it.
  • Travesty3
    Travesty3 about 9 years
    @Andrew: I realize this post is four and a half years old, but it's not so obvious to those that aren't used to working with SWFObject. I'm using an Angular add-on for file uploads that resorts to Flash if HTML5 is not supported, and wanted to display a message if Flash was not detected. It wasn't obvious to me that SWFObject was a library that needed to be loaded, or if it was auto-loaded via the installation of Flash Player in the browser. Thank you for clarifying in your comment, but please consider adding it to your answer.
  • Andrew Ensley
    Andrew Ensley about 9 years
    @Travesty3 fair point. I hadn't considered that. Answer updated.
  • Nikhil Bansal
    Nikhil Bansal over 8 years
    I'd like to note that Adobe uses SWFObject to report user Flash version on adobe.com/software/flash/about
  • Sabin Chacko
    Sabin Chacko over 7 years
    This should be the answer... :)