博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

如何判断客户端是否安装了指定的ActiveX控件

Posted on 2006-07-06 14:43  单摆  阅读(2407)  评论(0编辑  收藏  举报
今天遇到了一些问题,即如何判断客户端是否安装了某个插件?最常见的就是使用DHTML Behavior中的两个方法:isComponentInstalled

但是msdn上有这么一句话:Only Microsoft Internet Explorer components are detected by this method. If a component identifier of a third-party component is specified, the method returns false。

为了解决问题,自己花了1个小时写了下面通用的方法,供大家参考:
<HTML>
<HEAD>
<script type="text/javascript">
function detectPlugin(CLSID,functionName)
{
    
var pluginDiv = document.createElement("<div id=\"pluginDiv\" style=\"display:none\"></div>")
    document.body.insertBefore(pluginDiv);
    pluginDiv.innerHTML 
= '<object id="objectForDetectPlugin" classid="CLSID:'+ CLSID +'"></object>';
    
try
    {
        
if(eval("objectForDetectPlugin." + functionName) == undefined)
        {
            pluginDiv.removeNode(
true);//删除pluginDiv及其所有的子元素
            return false;
        }
        
else
        {
            pluginDiv.removeNode(
true);//删除pluginDiv及其所有的子元素
            return true;
        }
    }
    
catch(e)
    {
        
return false;
    }
}
function check()
{
    
if(detectPlugin(document.all.txtCLSID.value,document.all.txtFunctionName.value))
        alert('该控件已经安装') 
    
else 
        alert('该控件未安装');
}
</script> 
</HEAD>

<BODY>
CLSID:
<INPUT TYPE="text" NAME="txtCLSID" id="txtCLSID" value="22D6F312-B0F6-11D0-94AB-0080C74C7E95">属性或方法:<INPUT TYPE="text" NAME="txtFunctionName" id="txtFunctionName" value="AutoStart">
<br><INPUT TYPE="submit" value="判断" onclick="check();return false;">
<BODY>
</HTML>


这里大家可以看到只要知道activex控件的classid和该控件所支持的方法或属性就可以判断客户端是否安装了该控件。
这里只是提出了一个解决方案,有更好的建议请留言。谢谢!