js創建調用ocx對象的幾種方法,檢測ocx是否可用


1:創建ActiveX對象
   在web中使用ActiveX組件有兩種方法,一是如下
<HTML>
<HEAD>
<TITLE>rep_print_medcan.CAB</TITLE>
</HEAD>
<BODY>
<OBJECT ID="Rep_Print_MedCan21"
CLASSID="CLSID:C0558D51-6AFD-11D5-BF5C-005070024001"
CODEBASE="rep_print_medcan.CAB#version=1,0,0,25">
</OBJECT>
</BODY>
</HTML>
OBJECT標簽中,通過ID指點了對象的ID號,javascript可以使用該ID號來引用該對象,而classid用於標識這個組件,每個ActiveX組件都有唯一一個用於表示他的ID號。而codebase指定的程序下載到本地(在系統目錄的“Downloaded Program Files”子目錄下),然后調用組件程序的自注冊入口函數注冊到當前系統中,以后的創建都在本地進行,不再涉及codebase屬性
 使用以上的方法來創建ActiveX對象,有些參數設置需要手抖的填寫,麻煩,

2、另外可以使用javascript的ActiveXObject對象來創建,一般過程是var act=new ActiveXObject(proid),proid為該ActiveX的標識號,是Classid的另一種表示方法

其中proid指在ocxctrl.cpp文件下的:

 

// 初始化類工廠和 guid

IMPLEMENT_OLECREATE_EX(CtestOCX2Ctrl, "TESTOCX2.testOCX2Ctrl.1",
 0xe0d5a597, 0x336b, 0x4829, 0x84, 0xe0, 0x77, 0x43, 0x91, 0xc8, 0xe8, 0xdb)

 

紅色表示部分。

3、

         var _tempPlayer = null;
         _tempPlayer = window.document.createElement("object");
         _tempPlayer.classid = "clsid:358DE739-F39F-4832-9458-80FDE806F845";
         _tempPlayer.id = "realtime_player"+i; // 唯一標識
         _tempPlayer.using = false; // 是否正在使用
         _tempPlayer.style.width = "100%";
         _tempPlayer.style.height = "100%";           

或者:
            /*var _tempPlayer = window.document.createElement("object");
            _tempPlayer.setAttribute("id","realtime_player"+i);
            _tempPlayer.setAttribute("classid","clsid:358DE739-F39F-4832-9458-80FDE806F845");
            _tempPlayer.setAttribute("width","100%");
            _tempPlayer.setAttribute("height","100%");
            _tempPlayer.using = false;  //>是否正在使用 

4、檢測ocx是否可用的方法

在js下是 var o = new ActiveXObject(ProgID);
IE下若ocx存在是object,否則是null
即:

var o = new ActiveXObject(ProgID);
if(o)

{

//存在

}

else

{

//不存在

}


 

<HTML>
<HEAD>
<TITLE>New Page</TITLE>

<script language="javascript" type="text/javascript">
    //var a1 = new ActiveXObject("TESTOCX1.testOCX1Ctrl.1");
    var ocx;
    function createOcx() {
        ocx = document.createElement("object");
        ocx.setAttribute("id", "ocx1");
        ocx.setAttribute("height", 100);
        ocx.setAttribute("width", 51);
        ocx.setAttribute("classid", "clsid:DA44C63F-4844-483F-9DA0-7129E1A7F05B");
    }

    function DestoryOcx() {
         delete ocx;
    }

    function AddToDiv() {
        var div = document.getElementById("divOcxContainer");
        div.appendChild(ocx);

        ocx.Method2();
    }

    function RemoveFromDiv() {
        var div = document.getElementById("divOcxContainer");
        var ocx1 = document.getElementById("ocx1");
        div.removeChild(ocx1); 
    }

    window.onload = function() {

    }


    window.onunload = function() {

    }

</script>

</HEAD>
<BODY>
<div id="divOcxContainer">
</div>
<input type="button" id="btn1" name="btn1" value="創建ocx" title="創建ocx" onclick="createOcx()"></input>
<input type="button" id="btn2" name="btn2" value="銷毀ocx"  title="銷毀ocx" onclick="DestoryOcx()"></input>
<input type="button" id="Button1" name="btn2" value="添加到div"  title="添加到div" onclick="AddToDiv()"></input>
<input type="button" id="Button2" name="btn2" value="從div移除"  title="從div移除" onclick="RemoveFromDiv()"></input>
<OBJECT ID="testOCX1" WIDTH=100 HEIGHT=51
 CLASSID="CLSID:DA44C63F-4844-483F-9DA0-7129E1A7F05B">
    <PARAM NAME="_Version" VALUE="65536">
    <PARAM NAME="_ExtentX" VALUE="2646">
    <PARAM NAME="_ExtentY" VALUE="1323">
    <PARAM NAME="_StockProps" VALUE="0">
</OBJECT>

<OBJECT ID="testOCX2" WIDTH=100 HEIGHT=51
 CLASSID="CLSID:DA44C63F-4844-483F-9DA0-7129E1A7F05B">
    <PARAM NAME="_Version" VALUE="65536">
    <PARAM NAME="_ExtentX" VALUE="2646">
    <PARAM NAME="_ExtentY" VALUE="1323">
    <PARAM NAME="_StockProps" VALUE="0">
</OBJECT>

</BODY>
</HTML>

 

參考html文件代碼:http://files.cnblogs.com/lidabo/Page1.rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM