Tuesday, 11 August 2015

Detecting Flash Plugin with JavaScript

It's nothing fancy. It certainly will not work for Internet Explorer's old versions.
I simply needed something to disable certain functionality if Flash was not installed on Tablets/iPads.
If you need to detect an iPad or you're looking for a robust plugin to detect Flash you're better off using media queries or some JavaScript Flash library.

Here it is:

CoffeeScript version

isFlashInstalled = ->
      try
        flashPlugin = _.filter(navigator.plugins, (el) -> return el.description.match(/.*shockwave flash*/i))
        flashPlugin != null && flashPlugin.length != 0
      catch err
        console.log("error in detecting flash")
      flashPlugin != null and flashPlugin.length != 0

JavaScript translated version

var isFlashInstalled = function() {

        var err, flashPlugin;
        try {
          flashPlugin = _.filter(navigator.plugins, function(el) {
            return el.description.match(/.*shockwave flash*/i);
          });
          flashPlugin !== null && flashPlugin.length !== 0;
        } catch (_error) {
          err = _error;
          console.log("error in detecting flash");
        }
        return flashPlugin !== null && flashPlugin.length !== 0;
      }

Here I'm using underscore js for the array filtering, you can off course use a plain for loop or array forEach method.

No comments:

Post a Comment