开发者

IE Automation - Calling a client JavaScript **method** from server-side C#

开发者 https://www.devze.com 2023-01-15 18:44 出处:网络
Given the following C# code: public object CallJavaScriptFunction(string functionName, params object[] args)

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 somethi开发者_如何转开发ng 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消