首先在本机通过regsvr32命令注册ActiveX控件,然后确保ActiveX控件能正确在IE中加载,IE7、IE8对安全控制得比较严,设置起来有点烦。
1. JavaScript调用ActiveX控件的示例代码:
2. JavaScript函数和ActiveX控件事件关联
1. JavaScript调用ActiveX控件的示例代码:
<!
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 >
< title > test </ title >
</ head >
< body >
< object id ="ctl1" classid ="clsid:A2B80A6E-42FA-4730-AEB2-B1FB38D2C8D1" width ="100" height ="50" >
</ object >
< script language ="javascript" >
document.getElementById( " ctl1 " ).sayHello();
</ script >
</ body >
</ html >
说明:ActiveX控件的classid需要通过查找注册表获得,然后通过getElementById()方法获取对象后,就可以像调用普通的JS函数一样使用了
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > test </ title >
</ head >
< body >
< object id ="ctl1" classid ="clsid:A2B80A6E-42FA-4730-AEB2-B1FB38D2C8D1" width ="100" height ="50" >
</ object >
< script language ="javascript" >
document.getElementById( " ctl1 " ).sayHello();
</ script >
</ body >
</ html >
2. JavaScript函数和ActiveX控件事件关联
<!
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 runat ="server" >
< title > test </ title >
</ head >
< body >
< script for ="ctl1" language ="JavaScript" event ="receiveMessageEvent(nFlag)" >
alert(nFlag);
</ script >
< object id ="ctl1" classid ="clsid:A2B80A6E-42FA-4730-AEB2-B1FB38D2C8D1" width ="100" height ="50" >
</ object >
</ body >
</ html >
说明:JavaScript函数要和ActiveX控件的事件进行关联,获取事件的返回值,主要通过<script for="控件ID" event="事件函数" language="javascript">来声明,事件关联函数要放在ActiveX控件代码的上面,否则接收不到事件通知。
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > test </ title >
</ head >
< body >
< script for ="ctl1" language ="JavaScript" event ="receiveMessageEvent(nFlag)" >
alert(nFlag);
</ script >
< object id ="ctl1" classid ="clsid:A2B80A6E-42FA-4730-AEB2-B1FB38D2C8D1" width ="100" height ="50" >
</ object >
</ body >
</ html >