1

Given the following C# code:

    public object CallJavaScriptFunction(string functionName, params object[] args)
    {
        object script = Document.Script;
        var result = script.GetType().InvokeMember(functionName, BindingFlags.InvokeMethod, null, script, args);

        return result;
    }

And the following client-side JavaScript block:

function someFunction() {
    alert('This is only a test!');
}

var someObj = {
    someMethod: function() {
        alert('This is another test!');
    }
}

The following server-side block executes successfully:

CallJavaScriptFunction("someFunction");

But this will throw a DISP_E_UNKNOWNNAME:

CallJavaScriptFunction("someOBj.someMethod");

Obviously I'm doing something wrong here - probably there is another way of calling InvokeMember on JavaScript instance methods, but I was not able to find out how.

Any thoughts? Any help will be appreciated.

1
  • 1
    Can you tell me in object script = Document.Script;, what's the namespace of Document? Commented Sep 13, 2010 at 15:18

1 Answer 1

2

You need to invoke the someObj property, then invoke the someMethod method on the value of the property

Sign up to request clarification or add additional context in comments.

9 Comments

I've never used .NET, and all the web app development i've dont is on PHP. Naturally, calling js code from the server (and indeed being able to do so) seems blasphemous to me. Can you please cite a couple of use cases where this might be required? Thanks.
@Here: He's apparently automating a server-side instance of the WebBrowser control displaying a (presumably) different page. I hope he knows what he's doing; that's generally a bad (and slow) idea.
@Here: This will be used for system tests automation - think of something like Selenium tests (seleniumhq.org). So I guess I've not commited any mortal sin here... :-)
@SLaks: Oh he is using WebBrowser. Just now I really feel confused when I saw his code. Now it's quite clear. haha~
@rsenna: If you're using the WinForms WebBrowser object, you should try the document's InvokeScript method instead.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.