How can I call a method on a custom object created in JavaScript using C#?

Posted by just in case on Stack Overflow See other posts from Stack Overflow or by just in case
Published on 2010-06-17T16:33:35Z Indexed on 2010/06/18 12:33 UTC
Read the original article Hit count: 289

Filed under:
|
|
|

I have a WebBrowser control. I have added some JavaScript into the head tag and I can see it is working as expected by adding an alert. Inside of this js I am creating a function and adding some members to it's prototype like so:

function test() {
}

test.prototype.run = function() {
    alert('success!')
}

function createTest() {
    return new test()
}

Then back inside of C# I am doing:

dynamic test = this.browser.InvokeScript("createTest");
test.run();

I can see that the test object is some ComObject but when I call run() nothing happens. I get no error but nothing happens. Does anyone know how to call this type of custom object?

Also suppose I wanted to get rid of the createTest() method, how can I create a new instance of test from C#?

Also, for bonus points, is there anything special I need to know about attaching events to this custom object (on say a 'complete' member) such that it will callback into my C# code?

© Stack Overflow or respective owner

Related posts about c#

Related posts about JavaScript