From a046be80fe9cbd2538e5a80fc3737dc5ea827d60 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sun, 17 Nov 2013 18:20:05 -0500 Subject: [PATCH] port over article http://brett.is/writing/about/detect-flash-with-javascript --- .../detect-flash-with-javascript/index.md | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 contents/writing/about/detect-flash-with-javascript/index.md diff --git a/contents/writing/about/detect-flash-with-javascript/index.md b/contents/writing/about/detect-flash-with-javascript/index.md new file mode 100644 index 0000000..acf2de3 --- /dev/null +++ b/contents/writing/about/detect-flash-with-javascript/index.md @@ -0,0 +1,62 @@ +--- +title: Detect Flash with JavaScript +author: Brett Langdon +date: 2013-06-05 +template: article.jade +--- + +Quick, easy and lightweight way to detecting flash support in clients. + +--- + +Recently I had to find a a good way of detecting if Flash +is enabled in the browser, there are the two main libraries +Adobe Flash Detection Kit +and SWFObject +which are both very good at detecting whether Flash is enabled as well as getting +the version of Flash installed and useful for dynamically embedding and manipulating +swf files +in your web application. But all I needed was a **yes** or a **no** to whether +Flash was there or not without the added overhead of unneeded code. +My goal was to wrote the least amount of JavaScript while still being able +to detect cross browser for Flash. + +```javascript +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; +} +``` + +For those unfamiliar with the tilde (~) operator in javascript, please read +this article, +but the short version is, used with indexOf these two lines are equivalent: + +```javascript +~navigator.appVersion.indexOf("MSIE") +navigator.appVersion.indexOf("MSIE") != -1 +``` + +To use the above function: + +```javascript +if(detectflash()){ + alert("Flash is enabled"); +} else{ + alert("Flash is not available"); +} +``` + +And that is it. Pretty simple and a much shorter version that the alternatives, +compressed and mangled I have gotten this code to under 400 Bytes. +I tested this code with IE 5.5+, Firefox and Chrome without any issues.