前文已經介紹了《使用IE測試COM》,那麼我們用一個實際的實例來使用他。
在這之前,我們需要了解2個Javascript函數:
- replace(regexp/substr,replacement)
使用這個函數來替換多餘的空格。
str.replace(/\s/g, "");
- split(separator,howmany)
字符串轉換成字符數組。
string.split(",");
一、js部分
我們把所有的方法放到一個對象(RepoInfo)中。
var RepoInfo = { };
1. 獲取對象
RepoInfo.getObj = function(objName) {
return new ActiveXObject(objName);
};
2. 獲取數組
RepoInfo.getArray = function(str) {
var ret = new Array(16);
var string = str.replace(/\s/g, "");
ret = string.split(",");
return ret;
};
3. 解碼字串
(1)輸入字串校驗
(2)輸出框清空
(3)執行COM中的方法
RepoInfo.decode = function(string) {
if (string == null) return;
var obj = RepoInfo.getObj("repoInfo.LabelInfo");
var edtView = document.getElementById("edtView");
edtView.value = " ";
var str = RepoInfo.getArray(string);
if (obj != null)
edtView.value = obj.getInfo(str);
};
二、html部分
1. 按鈕事件
從輸入框獲取字串作爲js方法的輸入參數,注意:兩種引號的配合使用。
<input type="button" id="btnOk" value="執行數據解析" onClick="RepoInfo.decode(document.getElementById('edtData').value)" />
2. 頁面效果
標籤數據:
執行結果:
三、源代碼(含js代碼)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>報文數據信息 - IE</title>
<script type="text/javascript">
var RepoInfo = {
};
RepoInfo.getObj = function(objName) {
return new ActiveXObject(objName);
};
RepoInfo.getArray = function(str) {
var ret = new Array(16);
var string = str.replace(/\s/g, "");
alert(string);
ret = string.split(",");
return ret;
};
RepoInfo.decode = function(string) {
if (string == null) return;
var obj = RepoInfo.getObj("repoInfo.LabelInfo");
var edtView = document.getElementById("edtView");
edtView.value = " ";
var str = RepoInfo.getArray(string);
if (obj != null)
edtView.value = obj.getInfo(str);
};
</script>
</head>
<body>
<textarea name="note" cols="98" rows="7" readonly="readonly">
注意:本測試只能運行在IE瀏覽器!
首先,要註冊Dll(regsvr32 repoInfo.dll);
然後,從“BinJiang_2005.rep”(濱江站),複製行數據到“標籤數據”框。
示例(固定格式):
0xD3, 0x05, 0x94, 0x84, 0x00, 0x13, 0x51, 0x2F, 0x59, 0x34, 0x57, 0x45, 0x58, 0x50, 0x41, 0x98</textarea>
<br>
<br>
標籤數據:<input type="text" id="edtData" size="98" />
<br>
<br>
執行結果:<input type="text" id="edtView" size="50" />
<br>
<br>
<input type="button" id="btnOk" value="執行數據解析" onClick="RepoInfo.decode(document.getElementById('edtData').value)" />
</body>
</html>
編後話:
國內只有兩家正規的鐵路車號開發機構(所/企業):遠望谷和威克公司。
參考文檔(W3School):
1. replace 方法
2. RegExp 對象
3. split 方法