铁路车号读取 - IE

来源:互联网 发布:哪些外卖软件首单立减 编辑:程序博客网 时间:2024/04/28 14:32

    前文已经介绍了《使用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 方法

0 0
原创粉丝点击