Search code examples
javascriptvb.netcom-interop

Invoke a javascript function with VB.net through COM Interop


I have a VB.net class registered for COM interop which I'm instantiating within an HTML page using the following code:

<script type="text/javascript">
var MyClass = new ActiveXObject("Namespace.TestClass");
</script>

I can call methods on it just fine, but suppose I want to set a javascript function as a property, like so:

MyClass.TestFunction = function () { alert("It worked!"); }

How would I set my vb.net code up to be able to fire that function? This is how MSXML works in javascript for XMLHttpRequest objects, you can set

XHR.onreadystatechange = function () {}

I'm looking for a similar implementation in my class.


Solution

  • After trying for a while, we managed to figure out a solution that worked pretty well. Since we're setting a javascript function to the property, all the properties and methods on that function are made available to the VB.net, including the javascript standard methods, call and apply:

    (function () { alert("Hello"); }).call();
    

    The solution was to just invoke the call method in the VB.net code and it seems to work pretty well.