Skip to content

如何使用JavaScript检测浏览器是否支持flash #30

Open
@gnipbao

Description

@gnipbao

image

背景

在视频网站经常见到flash播放器需要加载flash插件才能播放,那么如何检测浏览器是否已有插件或者以经下载。

原理

其实就是利用了浏览器插件检测的办法。IE检查插件是否安装,通过创建ActiveXObject来实现,而FF,Chrome等浏览器,是通过创建navigator.plugins来实现。

检测方法

很简单直接看代码

function flashCheck() {
    let swf,
        swf_ver,
        hasFlash = false,
        ver;
    try {
        if (document.all) {
            swf = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
            if (swf) {
                hasFlash = true;
                swf_ver = swf.GetVariable('$version');
            }

        } else {
            if (navigator.plugins && navigator.plugins.length > 0) {
                swf = navigator.plugins['Shockwave Flash'];
                if (swf) {
                    hasFlash = true;
                    swf_ver = swf.description;
                }
            }
        }
        if (typeof (swf_ver) !== 'string') {
            swf_ver = '';
        }
        ver = (swf_ver || '0 r0').match(/\d+/g);
    }
    catch (e) {

    }
    return {
        hasFlash, // has flash
        ver // flash version
    }
}

如果浏览器没有安装flash可以通过下面方式开启, 即一个a标签_blank打开即可。
💖💖💖💖💖💖💖💖💖💖下载Flash💖💖💖💖💖💖💖💖💖💖
have a fun day!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions