Domanda

Qualcuno ha un esempio di script che può funzionare in modo affidabile su IE / Firefox per rilevare se il browser è in grado di visualizzare contenuti flash incorporati. Dico in modo affidabile perché so che non è possibile il 100% delle volte.

È stato utile?

Soluzione

SWFObject è molto affidabile. L'ho usato senza problemi per un bel po '.

Altri suggerimenti

Sono d'accordo con Max Stewart . SWFObject è la strada da percorrere. Vorrei integrare la sua risposta con un esempio di codice. Questo dovrebbe iniziare:

Assicurati di aver incluso il file swfobject.js (scaricalo qui ):

<script type="text/javascript" src="swfobject.js"></script>

Quindi usalo in questo modo:

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");
}

Sostituisci " 9.0.115 " con qualsiasi versione minima del flash di cui hai bisogno. Ho scelto 9.0.115 come esempio perché è la versione che ha aggiunto il supporto h.264.

Se il visitatore non ha flash, riporterà una versione flash di "0.0.0", quindi se vuoi solo sapere se hanno flash, usa:

if(swfobject.hasFlashPlayerVersion("1"))
{
    alert("You have flash!");
}
else
{
    alert("You do not flash :-(");
}

So che questo è un vecchio post, ma ho cercato per un po 'e non ho trovato nulla.
Ho implementato la Libreria di rilevamento flash JavaScript . Funziona molto bene ed è documentato per un rapido utilizzo. Mi ci sono voluti letteralmente 2 minuti. Ecco il codice che ho scritto nell'intestazione:

<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>        

Puoi utilizzare compilatore di chiusura per generare un rilevamento flash su più browser:

// ==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');
}

che risulta nel seguente "compilato" codice:

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");

Versione minima che abbia mai usato (non controlla la versione, solo Flash Plugin):

var hasFlash = function() {
    return (typeof navigator.plugins == "undefined" || navigator.plugins.length == 0) ? !!(new ActiveXObject("ShockwaveFlash.ShockwaveFlash")) : navigator.plugins["Shockwave Flash"];
};

Libreria di rilevamento flash JavaScript di Carl Yestrau, qui:

http://www.featureblend.com/javascript-flash-detection-library.html

... potrebbe essere quello che stai cercando.

Forse il kit di rilevamento di Adobe Flash Player potrebbe essere utile qui?

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

Rilevare e incorporare Flash in un documento Web è un compito sorprendentemente difficile.

Sono rimasto molto deluso dal markup di qualità e non conforme agli standard generato da SWFObject e dalle soluzioni Adobe. Inoltre, i miei test hanno riscontrato che l'aggiornamento automatico di Adobe era incoerente e inaffidabile.

La libreria di rilevamento flash JavaScript (Flash Detect) e JavaScript Flash HTML Generator Library (Flash TML) sono una soluzione di markup leggibile, gestibile e conforme agli standard .

- " Luke legge la fonte! "

Codice per una riga isFlashExists :

<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>

Nota che esiste un'alternativa come questa: swfobject.getFlashPlayerVersion();

Visualizza la fonte su http://whatsmy.browsersize.com (righe 14-120).

Ecco il browser incrociato astratto codice su jsbin solo per rilevamento flash , funziona su: FF / IE / Safari / Opera / Chrome.

che dire di:

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;
};

Se sei interessato a una soluzione Javascript pura, ecco quella che copio da 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;
}

Se vuoi solo verificare se il flash è abilitato, questo dovrebbe essere sufficiente.

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;

}

Nota: evitare di selezionare enabledPlugin , alcuni browser per dispositivi mobili hanno plug-in flash tap-to-enable e attiveranno falsi negativi.

Per creare un oggetto Flash conforme allo standard (comunque con JavaScript), ti consiglio di dare un'occhiata a

Oggetti flash discreti (UFO)

http://www.bobbyvandersluis.com/ufo/index.html

Ho creato un piccolo .swf che reindirizza. Se il browser è abilitato per flash, reindirizzerà.

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");

}

}

Utilizzando la libreria goog.require del compilatore di Google Closure ('goog.userAgent.flash') ho creato queste 2 funzioni.

hasFlash booleano ()

Restituisce se il browser ha il 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;
}

booleano isFlashVersion (versione)

Restituisce se la versione flash è maggiore della versione fornita

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)
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top