97

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.

1
  • I see you've already accepted joeri's answer, but you really ought to seriously consider swfObject. It's a lot more robust and less bloaty. Oct 2, 2008 at 8:32

16 Answers 16

110

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 :-(");
}
6
  • 9
    great. I was struggling to find a really simple example of simply detecting any flash installed. Thanks. Mar 13, 2011 at 10:58
  • 2
    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. :)
    – kontur
    Mar 9, 2012 at 12:15
  • 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 }
    – E-comm
    Apr 26, 2015 at 17:41
  • 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. Apr 26, 2015 at 18:19
  • @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.
    – Travesty3
    Jun 11, 2015 at 3:40
94

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

6
  • Same here, SWFObject works great for me as well (used to be called FlashObject, but Adobe threw a hissy fit)
    – davr
    Oct 1, 2008 at 20:00
  • 17
    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');
    – jrz
    Aug 24, 2012 at 13:50
  • 2
    If anyone is interested I tested some of these cases on jsperf. SWFObject came out the fastest. Dec 20, 2012 at 8:37
  • 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.
    – Avatar
    Mar 30, 2014 at 20:59
  • 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.
    – Akrikos
    Aug 19, 2014 at 14:24
36

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>        
4
  • it now fails on Chrome and Firefox 6+!
    – balint
    Oct 10, 2011 at 22:41
  • Seems legit - the latest version works pretty well or me. Thank you, keep up the good work!
    – poitroae
    Feb 5, 2013 at 15:46
  • Thanks. I like this library because it doesn't require a sentinel swf. I hope you keep it up to date! Dec 30, 2013 at 21:24
  • This should be the answer... :) Nov 9, 2016 at 13:49
32

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");
5
  • 3
    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! May 10, 2012 at 6:53
  • 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. Aug 7, 2013 at 16:13
  • we are not using the first snippet. we are using the 2nd. which is 'library free'. Aug 7, 2013 at 20:59
  • This is the purest and most thorough answer I've seen that seems to cover all browsers. Thank you.
    – HartleySan
    Oct 3, 2013 at 13:07
  • 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 Nov 20, 2013 at 20:32
22

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"];
};
7
  • 2
    Nice and short, I like it! Jan 16, 2013 at 4:27
  • @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 ;)
    – tomasdev
    Feb 25, 2013 at 5:27
  • 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
    Jun 17, 2013 at 16:32
  • @Zensursula: I put a try{}catch() around the ActiveXObject and returned false in the exception clause. Now It is working for mee too
    – Zensursula
    Jun 17, 2013 at 16:44
  • 1
    @mch a ton of things could do that. but the by default security levels should not.
    – tomasdev
    Apr 13, 2015 at 23:25
8

Carl Yestrau's JavaScript Flash Detection Library, here:

http://www.featureblend.com/javascript-flash-detection-library.html

... may be what you're looking for.

7

Perhaps adobe's flash player detection kit could be helpful here?

http://www.adobe.com/products/flashplayer/download/detection_kit/

1
  • 6
    Would just like to point out: SWFObject is the successor to Adobe's detection kit linked here. Dec 30, 2010 at 19:34
5

Detecting and embedding Flash within a web document is a surprisingly difficult task.

I was very disappointed with the quality and non-standards compliant markup generated from both SWFObject and Adobe's solutions. Additionally, my testing found Adobe's auto updater to be inconsistent and unreliable.

The JavaScript Flash Detection Library (Flash Detect) and JavaScript Flash HTML Generator Library (Flash TML) are a legible, maintainable and standards compliant markup solution.

-"Luke read the source!"

5

Code for one liner isFlashExists variable:

<script type='text/javascript'
    src='//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'> </script>

<script type='text/javascript'>
   var isFlashExists = swfobject.hasFlashPlayerVersion('1') ? true : false ;
   if (isFlashExists) {
    alert ('flash exists');
   } else {
    alert ('NO flash');
   }
</script>

Note that there is an alternative like this: swfobject.getFlashPlayerVersion();

3

View the source at http://whatsmy.browsersize.com (lines 14-120).

Here is the abstracted cross browser code on jsbin for flash detection only, works on: FF/IE/Safari/Opera/Chrome.

4
  • Can you provide some code or at least a link as to how you achieved the answer on your site? Dec 18, 2012 at 15:04
  • @hitautodestruct If you view the source of the page, the answer is directly there, at the top of the JS block.
    – Ates Goral
    Dec 18, 2012 at 18:14
  • So essentially were talking about this code (jsbin link)? Dec 19, 2012 at 9:28
  • @hitautodestruct Yes, plus the detectObject() counterpart for IE.
    – Ates Goral
    Dec 19, 2012 at 15:15
3

what about:

var hasFlash = function() {
    var flash = false;
    try{
        if(new ActiveXObject('ShockwaveFlash.ShockwaveFlash')){
            flash=true;
        }
    }catch(e){
        if(navigator.mimeTypes ['application/x-shockwave-flash'] !== undefined){
            flash=true;
        }
    }
    return flash;
};
2

If you are interested in a pure Javascript solution, here is the one that I copy from Brett:

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;
}
1

If you just wanted to check whether flash is enabled, this should be enough.

function testFlash() {

    var support = false;

    //IE only
    if("ActiveXObject" in window) {

        try{
            support = !!(new ActiveXObject("ShockwaveFlash.ShockwaveFlash"));
        }catch(e){
            support = false;
        }

    //W3C, better support in legacy browser
    } else {

        support = !!navigator.mimeTypes['application/x-shockwave-flash'];

    }

    return support;

}

Note: avoid checking enabledPlugin, some mobile browser has tap-to-enable flash plugin, and will trigger false negative.

0

To create a Flash object standart-compliant (with JavaScript however), I recommend you take a look at

Unobtrusive Flash Objects (UFO)

http://www.bobbyvandersluis.com/ufo/index.html

0

Have created a small .swf which redirects. If the browser is flash enabled it will redirect.

package com.play48.modules.standalone.util;

import flash.net.URLRequest;


class Redirect {


static function main() {

    flash.Lib.getURL(new URLRequest("http://play48.com/flash.html"), "_self");

}

}
0

Using Google Closure compiler goog.require('goog.userAgent.flash') library I created this 2 functions.

boolean hasFlash()

Returns if browser has flash.

function hasFlash(){
    var b = !1;
    function c(a) {if (a = a.match(/[\d]+/g)) {a.length = 3;}}
    (function() {
    if (navigator.plugins && navigator.plugins.length) {
        var a = navigator.plugins["Shockwave Flash"];
        if (a && (b = !0, a.description)) {c(a.description);return;}
        if (navigator.plugins["Shockwave Flash 2.0"]) {b = !0;return;}
    }
    if (navigator.mimeTypes && navigator.mimeTypes.length && (a = navigator.mimeTypes["application/x-shockwave-flash"], b = !(!a || !a.enabledPlugin))) {c(a.enabledPlugin.description);return;}
    if ("undefined" != typeof ActiveXObject) {
        try {
            var d = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");b = !0;c(d.GetVariable("$version"));return;
        } catch (e) {}
        try {
            d = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");b = !0;
            return;
        } catch (e) {}
        try {
            d = new ActiveXObject("ShockwaveFlash.ShockwaveFlash"), b = !0, c(d.GetVariable("$version"));
        } catch (e) {}
    }
    })();
    return b;
}

boolean isFlashVersion(version)

Returns if the flash version is greater than provided version

function isFlashVersion(version) {
    var e = String.prototype.trim ? function(a) {return a.trim()} : function(a) {return /^[\s\xa0]*([\s\S]*?)[\s\xa0]*$/.exec(a)[1]};
    function f(a, b) {return a < b ? -1 : a > b ? 1 : 0};
    var h = !1,l = "";
    function m(a) {a = a.match(/[\d]+/g);if (!a) {return ""}a.length = 3;return a.join(".")}
    (function() {
        if (navigator.plugins && navigator.plugins.length) {
            var a = navigator.plugins["Shockwave Flash"];
            if (a && (h = !0, a.description)) {l = m(a.description);return}
            if (navigator.plugins["Shockwave Flash 2.0"]) {h = !0;l = "2.0.0.11";return}
        }
        if (navigator.mimeTypes && navigator.mimeTypes.length && (a = navigator.mimeTypes["application/x-shockwave-flash"], h = !(!a || !a.enabledPlugin))) {l = m(a.enabledPlugin.description);return}
        if ("undefined" != typeof ActiveXObject) {
            try {
                var b = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");h = !0;l = m(b.GetVariable("$version"));return
            } catch (g) {}
            try {
                b = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");h = !0;l = "6.0.21";return
            } catch (g) {}
            try {
                b = new ActiveXObject("ShockwaveFlash.ShockwaveFlash"), h = !0, l = m(b.GetVariable("$version"))
            } catch (g) {}
        }
    })();
    var n = l;
    return (function(a) {
        var b = 0,g = e(String(n)).split(".");
        a = e(String(a)).split(".");
        for (var p = Math.max(g.length, a.length), k = 0; 0 == b && k < p; k++) {
            var c = g[k] || "",d = a[k] || "";
            do {
                c = /(\d*)(\D*)(.*)/.exec(c) || ["", "", "", ""];d = /(\d*)(\D*)(.*)/.exec(d) || ["", "", "", ""];
                if (0 == c[0].length && 0 == d[0].length) {break}
                b = f(0 == c[1].length ? 0 : parseInt(c[1], 10), 0 == d[1].length ? 0 : parseInt(d[1], 10)) || f(0 == c[2].length, 0 == d[2].length) || f(c[2], d[2]);c = c[3];d = d[3]
            } while (0 == b);
        }
        return 0 <= b
    })(version)
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.