IE11中OCX控件的事件不能够被IE捕获的解决办法

本文介绍了在IE11中,由于移除了attachEvent,导致OCX控件触发的事件无法被捕获的问题。通过分析,发现IE11的addEventListenner未正确处理事件注册。提出通过强制IE10兼容模式或寻找其他工作绕过此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写了一个OCX控件,在OCX控件中触发一事件,在IE11之前的版本使用attachEvent注册一个回调函数后IE11可以捕获到事件,但IE11中提示attachEvent不支持.

In IE11, the attachEvent has been dropped in favour of addEventListener.

Nevertheless, when I replace all the attachEvent instances that were used to bind to events triggered by the activeX, such events are no longer triggered.

Here are some html code examples that illustrate the problem.

(I have created a simple activeX based on an MFC activeX control that simply triggers an event inside OnCreate. This event sends 2 strings as parameters. If you need the code or even the ocx file for the activeX, please let me know.)


Test 1 uses the "for" tag to setup the callback to the activeX event. This is successful on all versions IE8, 9, 10 and 11.

<!DOCTYPE html>
<html>
	<head>
		<title>TestEvent Example HTML</title>
		<meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
		<script language='javascript' for="testAxEvent" event="testEvent(szType, szValue)">
		// Test 1 - statically load the script (This is the basis for the hack)
		// Works on IE8, IE9, IE10 and IE11
			var MSG = document.getElementById("msg");
			MSG.innerHTML = szType + " : " + szValue;
		</script>
	</head>
	<body>
		<table>
			<tr>
				<td>
					<OBJECT ID='testAxEvent' CLASSID='CLSID:BA19A985-C93E-491B-B801-2EB7F903C8CE' codebase="testAxEvent.cab"></OBJECT>
				</td>
			</tr>
			<tr><td  height='30'></td></tr>
			<tr>
				<td align=center><font size=5><div id='msg'></div></font>	
			</tr>
		</table>
	</body>
</html>


Test 2 uses attachEvent to bind the callback to the object element. The callback is correctly called on IE8, 9 and 10 but not on IE11, as was expected.

<!DOCTYPE html>
<html>
	<head>
		<title>TestEvent Example HTML</title>
		<meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
	</head>
	<body>
		<table>
			<tr>
				<td>
					<OBJECT ID='testAxEvent' CLASSID='CLSID:BA19A985-C93E-491B-B801-2EB7F903C8CE' codebase="testAxEvent.cab" width='120' height='80'></OBJECT>
				</td>
			</tr>
			<tr><td  height='30'></td></tr>
			<tr>
				<td align=center><font size=5><div id='msg'></div></font>	
			</tr>
		</table>
	</body>
	<script>
		// Test 2 - attachEvent
		// Works on IE8, IE9 and IE10
		// Does not work on IE11
		function onTestEvent(szType, szValue)
		{
			var MSG = document.getElementById("msg");
			MSG.innerHTML = szType + " : " + szValue;
		}
		var elem = document.getElementById("testAxEvent");
		elem.attachEvent("testEvent", onTestEvent);
	</script>
</html>

Test 3 replaces attachEvent with addEventListener. Still the event is not triggered on IE11.

<!DOCTYPE html>
<html>
	<head>
		<title>TestEvent Example HTML</title>
		<meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
	</head>
	<body>
		<table>
			<tr>
				<td>
					<OBJECT ID='testAxEvent' CLASSID='CLSID:BA19A985-C93E-491B-B801-2EB7F903C8CE' codebase="testAxEvent.cab" width='120' height='80'></OBJECT>
				</td>
			</tr>
			<tr><td  height='30'></td></tr>
			<tr>
				<td align=center><font size=5><div id='msg'></div></font>	
			</tr>
		</table>
	</body>
	<script>
		function onTestEvent(szType, szValue)
		{
			var MSG = document.getElementById("msg");
			MSG.innerHTML = szType + " : " + szValue;
		}
		var elem = document.getElementById("testAxEvent");
		// Test 3 - addEventListener
		// Does not work on IE11 !
		elem.addEventListener("testEvent", onTestEvent, true);
		elem.addEventListener("testEvent", onTestEvent, false);
	</script>
</html>

We've been experiencing the same issue. We use an ActiveX component to control machinery via COM port. As many of our customers have recently received IE 11 as an automatic update, we have been receiving reports that the tool no longer works as expected. The ActiveX component is quite obviously running, because the machinery is being controlled, but none of the events produced are being registered in the browser window, reglardless of whether `attachEvent` or `addEventListener` is used.

Fortunately, there is a simple work-around: force IE 10 emulation, by adding the following to the document's `<head>`:

<meta http-equiv="X-UA-Compatible" content="IE=10" />

and then use `attachEvent`, as before.


IE 11 里,OCX控件的FireEvent函数无效。 

原因是 CConnectionPoint类对象中的 m_pConnections为NULL值。

在原有的attachEvent会向OCX注册一个event,用于插件内部的回调;

IE 11 去掉了attachEvent函数,新加入了addEventListenner却并没有把注册的event对象加入插件的事件队列里!!

IE 11 既然是拿NPAPI作为内核的重新封装的浏览器,既然非要支持原有的OCX控件,为何却要闹出这种乌龙!


最终只能使用 for ... event来处理了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值