88

How can I use javascript/jQuery/etc to detect if Flash is installed and if it isn't, display a div that contains information informing the user that they need to install flash?

KingNestor
  • 59,315
  • 50
  • 115
  • 149

8 Answers8

159

If swfobject won't suffice, or you need to create something a little more bespoke, try this:

var hasFlash = false;
try {
    hasFlash = Boolean(new ActiveXObject('ShockwaveFlash.ShockwaveFlash'));
} catch(exception) {
    hasFlash = ('undefined' != typeof navigator.mimeTypes['application/x-shockwave-flash']);
}

It works with 7 and 8.

Slava Fomin II
  • 21,036
  • 19
  • 98
  • 176
Drewid
  • 1,991
  • 2
  • 13
  • 13
  • 2
    this works nice if you just want to detect if it is installed and not necessarily display a swf either way. – ctrlShiftBryan Oct 13 '10 at 20:03
  • 10
    Had to modify this to: var hasFlash = false; try { var fo = (navigator.mimeTypes && navigator.mimeTypes['application/x-shockwave-flash']) ? navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin : 0; if(fo) hasFlash = true; }catch(e){ if(navigator.mimeTypes ['application/x-shockwave-flash'] != undefined) hasFlash = true; }" – invertedSpear May 06 '11 at 00:13
  • 1
    that won't work on IE7, as you're not testing the activexobject part – Kevin May 27 '11 at 16:29
  • 1
    upvote for using 5 lines of JavaScript instead of using an entire library – Alex W Jun 17 '14 at 18:30
  • This will work for android mobile browser like firefox and chrome ? – Maniprakash Chinnasamy Oct 18 '14 at 12:23
  • nice solution, but in the catch case I prefer to check : `(typeof navigator.mimeTypes['application/x-shockwave-flash'] !== 'undefined')` – zooblin Jan 17 '16 at 07:00
  • solution was not working for ubuntu14.04 firefox(44.0) but @inverrtedSpear with your changes solution worked fine. – Aameer Feb 08 '16 at 13:52
  • This is only working if the user hasn't installed Flash Player, but it's not working if it's installed but deactivated. – Jose Ignacio Hita Jul 22 '16 at 05:06
  • What if the flash is out of date ? Safari sometimes says it's out of date and it won't work until you update. How would I check for that? – trainoasis Sep 02 '16 at 08:01
104

@Drewid's answer didn't work in my Firefox 25 if the flash plugin is just disabled but installed.

@invertedSpear's comment in that answer worked in firefox but not in any IE version.

So combined both their code and got this. Tested in Google Chrome 31, Firefox 25, IE 8-10. Thanks Drewid and invertedSpear :)

var hasFlash = false;
try {
  var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
  if (fo) {
    hasFlash = true;
  }
} catch (e) {
  if (navigator.mimeTypes
        && navigator.mimeTypes['application/x-shockwave-flash'] != undefined
        && navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin) {
    hasFlash = true;
  }
}
Vigneshwaran
  • 3,155
  • 6
  • 20
  • 33
26

Use swfobject. it replaces a div with the flash if it is installed. see: http://code.google.com/p/swfobject/

L84
  • 42,350
  • 55
  • 167
  • 243
Josh
  • 5,998
  • 1
  • 31
  • 54
18

You can use navigator.mimeTypes.

if (navigator.mimeTypes ["application/x-shockwave-flash"] == undefined)
    $("#someDiv").show ();
albertein
  • 23,752
  • 5
  • 51
  • 57
10

jqplugin: http://code.google.com/p/jqplugin/

$.browser.flash == true
L84
  • 42,350
  • 55
  • 167
  • 243
mhenry1384
  • 7,126
  • 4
  • 50
  • 69
4

You should also be able to use..

swfobject.getFlashPlayerVersion().major === 0

with the swfobject-Plugin.

empiric
  • 7,449
  • 6
  • 35
  • 44
2

I used Adobe's detection kit, originally suggested by justpassinby. Their system is nice because it detects the version number and compares it for you against your 'required version'

One bad thing is it does an alert showing the detected version of flash, which isn't very user friendly. All of a sudden a box pops up with some seemingly random numbers.

Some modifications you might want to consider:

  • remove the alert
  • change it so it returns an object (or array) --- first element is boolean true/false for "was the required version found on user's machine" --- second element is the actual version number found on user's machine
Mike
  • 21
  • 1
1

Very very minified version of http://www.featureblend.com/javascript-flash-detection-library.html (only boolean flash detection)

var isFlashInstalled = (function(){
var b=new function(){var n=this;n.c=!1;var a="ShockwaveFlash.ShockwaveFlash",r=[{name:a+".7",version:function(n){return e(n)}},{name:a+".6",version:function(n){var a="6,0,21";try{n.AllowScriptAccess="always",a=e(n)}catch(r){}return a}},{name:a,version:function(n){return e(n)}}],e=function(n){var a=-1;try{a=n.GetVariable("$version")}catch(r){}return a},i=function(n){var a=-1;try{a=new ActiveXObject(n)}catch(r){a={activeXError:!0}}return a};n.b=function(){if(navigator.plugins&&navigator.plugins.length>0){var a="application/x-shockwave-flash",e=navigator.mimeTypes;e&&e[a]&&e[a].enabledPlugin&&e[a].enabledPlugin.description&&(n.c=!0)}else if(-1==navigator.appVersion.indexOf("Mac")&&window.execScript)for(var t=-1,c=0;c<r.length&&-1==t;c++){var o=i(r[c].name);o.activeXError||(n.c=!0)}}()};  
return b.c;
    })();

if(isFlashInstalled){
    // Do something with flash
    }else{
    // Don't use flash  
        }
lucasgabmoreno
  • 881
  • 1
  • 9
  • 17