校验浏览器插件

由于各浏览器对插件支持的不同,有通过navagator对象,有通过vbscript的createboject方法校验,找到篇当前稍好一些的校验功能.ie支持得比较全面,发现firefox中检测还是有若干问题.当无法进行插件检测时,将插件下载地址显示出来做为临时解决方法

 

 

引用:http://developer.apple.com/internet/webcontent/detectplugins.html
1.detectPlugin方法进行浏览器对象navigator.plugins校验 返回是否找到插件
2.detectableWithVB->detectActiveXControl 允许vb校验的情况,采用activex创建对象形式创建目标
3.redirectCheck 重定向检测组件

 

<html> 
  <head>
  <script language="JavaScript"> 
  <!--
        // initialize a variable to test for JavaScript 1.1.
        // which is necessary for the window.location.replace method
        var javascriptVersion1_1 = false;
  // -->
    </script>

    <script language="JavaScript1.1"> 
  <!--
  javascriptVersion1_1 = true;
  // -->
    </script>

    <script language="JavaScript"> 
  <!--
        // initialize global variables
        var detectableWithVB = false;
        var pluginFound = false;
  alert(navigator.userAgent);
  //跳转到指定url链接
        function goURL(daURL) {
            // if the browser can do it, use replace to preserve back button
            if (javascriptVersion1_1) {
                window.location.replace(daURL);
            } else {
                window.location = daURL;
            }
            return;
        }
  //检测到插件且允许重定向,跳转到重定向地址同时运回插件是否找到
  //未查到插件且不可以重定向,跳转到下载页面,同时返回插件是否找到
        function redirectCheck(pluginFound, redirectURL, redirectIfFound) {
            // 检查重定向
            if (redirectURL && ((pluginFound && redirectIfFound) ||
  (!pluginFound && !redirectIfFound))) {
                //检测到已安装插件,之后转向对应资源链接 或未检测到插件,跳转到下载地址
                goURL(redirectURL);
                return pluginFound;
            } else {
                //只返回是否检测到结果
                return pluginFound;
            }
        }
  //是否允许检测,detectableWithVB 检测ie下是否支持vbscritp;
  //navigator.plugins检测浏览器是否支持navigator插件对象
        function canDetectPlugins() {
            if (detectableWithVB || (navigator.plugins && navigator.plugins.length > 0)) {
                return true;
            } else {
                return false;
            }
        }

  //检测flash插件
        function detectFlash(redirectURL, redirectIfFound) {
            pluginFound = detectPlugin('Shockwave', 'Flash');
            //如未找到尝试VisualBasic 检测方法
            if (!pluginFound && detectableWithVB) {
                pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
            }
   //tangwf
   if (!pluginFound && !detectableWithVB)
   {
    alert("无法进行Flash插件检测");
   }
            //检查重定向,同时返回是否查到状态
            return redirectCheck(pluginFound, redirectURL, redirectIfFound);
        }
  //检测Shockwave
        function detectDirector(redirectURL, redirectIfFound) {
            pluginFound = detectPlugin('Shockwave', 'Director');
            //如未找到尝试VisualBasic 检测方法
            if (!pluginFound && detectableWithVB) {
                pluginFound = detectActiveXControl('SWCtl.SWCtl.1');
            }
   //tangwf
   if (!pluginFound && !detectableWithVB)
   {
    alert("无法进行Director插件检测");
   }
            //检查重定向,同时返回是否查到状态
            return redirectCheck(pluginFound, redirectURL, redirectIfFound);
        }
  //检测QuickTime
        function detectQuickTime(redirectURL, redirectIfFound) {
            pluginFound = detectPlugin('QuickTime');
            //如未找到尝试VisualBasic 检测方法
            if (!pluginFound && detectableWithVB) {
                pluginFound = detectQuickTimeActiveXControl();
            }
   //tangwf
   if (!pluginFound && !detectableWithVB)
   {
    alert("无法进行QuickTime插件检测");
   }
            return redirectCheck(pluginFound, redirectURL, redirectIfFound);
        }

  //检测RealPlayer
        function detectReal(redirectURL, redirectIfFound) {
            pluginFound = detectPlugin('RealPlayer');
            //如未找到尝试VisualBasic 检测方法
            if (!pluginFound && detectableWithVB) {
                pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
  detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
  detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
            }
   //tangwf
   if (!pluginFound && !detectableWithVB)
   {
    alert("无法进行RealPlayer插件检测");
   }
            return redirectCheck(pluginFound, redirectURL, redirectIfFound);
        }

  //检测Windows Media Player
        function detectWindowsMedia(redirectURL, redirectIfFound) {
            pluginFound = detectPlugin('Windows Media');
            //如未找到尝试VisualBasic 检测方法
            if (!pluginFound && detectableWithVB) {
                pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
            }
   //tangwf
   if (!pluginFound && !detectableWithVB)
   {
    alert("无法进行Windows Media插件检测");
   }
            //twf alert(pluginFound);
            return redirectCheck(pluginFound, redirectURL, redirectIfFound);
        }

  //1.通过 navigator.plugins对象检测插件,用于支持navigator.plugins的浏览器
        function detectPlugin() {
            //传入参数
            var daPlugins = detectPlugin.arguments;
            // 返回值,默认false
            var pluginFound = false;
           

   //tangwf 监测各环境使用 ie firefox各浏览器中 navigator.plugins支持情况
   var s="/r/n";
   var i ;
   if (navigator.plugins && navigator.plugins.length > 0) {
     for (i = 0; i < navigator.plugins.length;i++)
     {
     s=s+i+":插件名称:"+navigator.plugins[i].name+"/r/n"+"插件描述"+navigator.plugins[i].description+"/r/n";
     }
   }
   //alert("detectPlugin方法检测navigator.plugins:"+"navigator.plugins.length:"+navigator.plugins.length+"/r/n"+s);
   //tangwf end 监测各环境使用


   // 存在navigator插件对象时
            if (navigator.plugins && navigator.plugins.length > 0) {
                //alert(daPlugins[0]+":"+daPlugins[1]+":navigator.plugins:"+navigator.plugins[0]);
                var pluginsArrayLength = navigator.plugins.length;
                // for each plugin...
                for (pluginsArrayCounter = 0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++) {
                    // loop through all desired names and check each against the current plugin name
                    var numFound = 0;
                    for (namesCounter = 0; namesCounter < daPlugins.length; namesCounter++) {
                        // if desired plugin name is found in either plugin name or description
                        if ((navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) ||
  (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0)) {
                            //插件名或描述已找到
                            numFound++;
                        }
                    }
                    //完成某一插件daPlugins(传入参数的校验)
                    //前后两段的参数名都找到时,返真,否则为假
                    if (numFound == daPlugins.length) {
      pluginFound = true;
      alert("完成某一插件检测:"+daPlugins[0]+":"+daPlugins[1]);
                        break;
                    }
                }
            }
            return pluginFound;
        } // detectPlugin


        // 当前为ie或wince环境下输出vb脚本块 实际上完成检测是否支持vb的变量detectableWithVB
        if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
            document.writeln('<script language="VBscript">');

            document.writeln('/'do a one-time test for a version of VBScript that can handle this code');
            document.writeln('detectableWithVB = False');
            document.writeln('If ScriptEngineMajorVersion >= 2 then');
            document.writeln(' detectableWithVB = True');
            document.writeln('End If');

            document.writeln('/'this next function will detect most plugins');
            document.writeln('Function detectActiveXControl(activeXControlName)');
            document.writeln(' on error resume next');
            document.writeln(' detectActiveXControl = False');
            document.writeln(' If detectableWithVB Then');
            document.writeln(' detectActiveXControl = IsObject(CreateObject(activeXControlName))');
            document.writeln(' End If');
            document.writeln('End Function');

            document.writeln('/'and the following function handles QuickTime');
            document.writeln('Function detectQuickTimeActiveXControl()');
            document.writeln(' on error resume next');
            document.writeln(' detectQuickTimeActiveXControl = False');
            document.writeln(' If detectableWithVB Then');
            document.writeln(' detectQuickTimeActiveXControl = False');
            document.writeln(' hasQuickTimeChecker = false');
            document.writeln(' Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
            document.writeln(' If IsObject(hasQuickTimeChecker) Then');
            document.writeln(' If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
            document.writeln(' detectQuickTimeActiveXControl = True');
            document.writeln(' End If');
            document.writeln(' End If');
            document.writeln(' End If');
            document.writeln('End Function');

            document.writeln('</scr' + 'ipt>');
        }
        // -->
    </script>
  </head> 
  
  <body> 
  
  <script language="JavaScript"> 
  
  document.write('<br><b>是否可以检测插件 Plugins:</b> ' + canDetectPlugins() + '<br><br>');
  
  if(canDetectPlugins()) {
  document.write('<b>是否支持 Shockwave Director:</b> ' + detectDirector() + '<br>' + 
  '<b>是否支持 Shockwave Flash:</b> ' + detectFlash() + '<br>' + 
  '<b>是否支持 QuickTime:</b> ' + detectQuickTime() + '<br>' +
  '<b>是否支持 RealPlayer:</b> ' + detectReal() + '<br>' +
  '<b>是否支持 Windows Media Player:</b> ' + detectWindowsMedia());
  }
  </script> 
  
  <noscript> 
  浏览器不支持 JavaScript, 当前无法校验插件
  </noscript> 
  
  
  </body> 
  </html>

 

由此方法延伸,可以在html5中实现siverlight一些插件的校验与友好提示

附:

1.通过classid检测

<script>
function testPlugin(){
sID="{D27CDB6E-AE6D-11cf-96B8-444553540000}"
 with(info){
   addBehavior('#default#clientcaps')
   info.innerText=isComponentInstalled(sID,"componentid")?'Flash插件已安装,版本:'+getComponentVersion(sID,"componentid"):"未安装插件"
 }
}
window.attachEvent("onload",testPlugin)
</script>
<span id="info">系统正在检测你的播放器版本...</span>

2.silverlight的一种检测方法

<script type="text/javascript">  
function CheckSilverlightInstalled()  
{  
var isSilverlightInstalled = false;   
try{  
try{  
var slControl = new ActiveXObject('AgControl.AgControl'); //检查IE  
isSilverlightInstalled = true;   
}  
catch(e){   
if(navigator.plugins["Silverlight Plug-In"]) //检查非IE  
{  
isSilverlightInstalled = true;   
}  
}  
}  
catch(e){}  
return isSilverlightInstalled;   
}  
alert("是否安装了 Silverlight 插件!" + CheckSilverlightInstalled())  
</script> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值