如何在Javascript中附加到COM事件 [英] How to attach to COM event in Javascript

查看:162
本文介绍了如何在Javascript中附加到COM事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了这个示例创建一个要在javascript中使用的COM事件。但是,我不是最终附加码的粉丝?

I have followed this example to create a COM event to be used in javascript. However, I am not a fan of the final attach code?

<script for="myComComponent" event="MyFirstEvent(args)" language="javascript">
    function myComComponent::MyFirstEvent(args) {
        alert('event');
    }
</script>

我想要的是更好地执行更多类似

What I would like is to be able to do something more like

//in the onload
myComComponent.MyFirstEvent = myJavascriptHandler;

//function setup
function myJavascript(args){
    alert('event');
}

这可以在我们较旧的C ++ COM事件中使用 IDispatch ,但是它不适用于新的C#代码。有没有办法在 onLoad 中挂钩事件?我只是想避免将JavaScript放在HTML ...

This works in our older C++ COM event using IDispatch, however it does not work with the new C# code. Is there any way to hook the event in the onLoad? I just would like to avoid putting the javascript in the HTML...

编辑:

我尝试过

myComComponent.addEventListener("MyFirstEvent", myJavascriptHandler, true); 

应该与点符号相同,这引发了不支持该事件的异常

which should be the same as the dot notation, and this threw an exception that the event was not supported.

推荐答案

我们将这样做:

myComComponent.attachEvent("MyFirstEvent", myJavascriptHandler);

这样做,删除代码不必在HTML中,并且相当干净。

This works, removes the code from having to be in the HTML and is fairly clean.

这篇关于如何在Javascript中附加到COM事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆