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.

davr
  • 18,877
  • 17
  • 76
  • 99
Ta01
  • 31,040
  • 13
  • 70
  • 99
  • 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. – matt lohkamp Oct 02 '08 at 08:32

16 Answers16

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 :-(");
}
Andrew Ensley
  • 11,611
  • 16
  • 61
  • 73
  • 9
    great. I was struggling to find a really simple example of simply detecting any flash installed. Thanks. – Brian Scott Mar 13 '11 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 09 '12 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 '15 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. – Andrew Ensley Apr 26 '15 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 '15 at 03:40
  • @Travesty3 fair point. I hadn't considered that. Answer updated. – Andrew Ensley Jun 12 '15 at 16:25
94

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

pranavcode
  • 747
  • 4
  • 12
Max Stewart
  • 3,573
  • 26
  • 28
  • Same here, SWFObject works great for me as well (used to be called FlashObject, but Adobe threw a hissy fit) – davr Oct 01 '08 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 '12 at 13:50
  • 2
    If anyone is interested I [tested some of these cases](http://jsperf.com/flash-detection/2) on jsperf. SWFObject came out the fastest. – hitautodestruct Dec 20 '12 at 08: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 '14 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 '14 at 14:24
  • I'd like to note that Adobe uses SWFObject to report user Flash version on http://www.adobe.com/software/flash/about/ – Michael Thompson Nov 20 '15 at 14:44
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>        
Community
  • 1
  • 1
Jon Clark
  • 369
  • 3
  • 2
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");
stewe
  • 41,820
  • 13
  • 79
  • 75
  • 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! – anonymous-one May 10 '12 at 06: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. – Andrew Ensley Aug 07 '13 at 16:13
  • we are not using the first snippet. we are using the 2nd. which is 'library free'. – anonymous-one Aug 07 '13 at 20:59
  • This is the purest and most thorough answer I've seen that seems to cover all browsers. Thank you. – HartleySan Oct 03 '13 at 13:07
  • You can also see the unminified version of this compiled code, including some helpful comments, over here: https://code.google.com/p/doctype-mirror/wiki/ArticleDetectFlash – James M. Greene Nov 20 '13 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"];
};
Tom Roggero
  • 5,777
  • 1
  • 32
  • 39
  • 2
    Nice and short, I like it! – mike nelson Jan 16 '13 at 04: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 ;) – Tom Roggero Feb 25 '13 at 05: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 '13 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 '13 at 16:44
  • This can create a error in IE depending on its security settings. – mch Apr 13 '15 at 17:23
  • 1
    @mch a ton of things could do that. but the by default security levels should not. – Tom Roggero Apr 13 '15 at 23:25
  • 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) – mch Apr 14 '15 at 07:47
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.

Kent Brewster
  • 2,480
  • 2
  • 22
  • 27
7

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

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

Joeri Sebrechts
  • 11,012
  • 3
  • 35
  • 50
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();

trante
  • 33,518
  • 47
  • 192
  • 272
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!"

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.

hitautodestruct
  • 20,081
  • 13
  • 69
  • 93
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
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;
};
Martin Bommeli
  • 101
  • 1
  • 2
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;
}
bizi
  • 3,398
  • 2
  • 27
  • 28
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.

bitinn
  • 9,188
  • 10
  • 38
  • 64
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");

}

}
Nazik
  • 8,696
  • 27
  • 77
  • 123
mike
  • 1
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

Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147
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)
}
chickens
  • 19,976
  • 6
  • 58
  • 55