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.
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.
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 :-(");
}
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");
Carl Yestrau's JavaScript Flash Detection Library, here:
http://www.featureblend.com/javascript-flash-detection-library.html
... may be what you're looking for.
Perhaps adobe's flash player detection kit could be helpful here?
http://www.adobe.com/products/flashplayer/download/detection_kit/
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!"
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.
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)
}