Special case when calling InvokeScript with WebBrowser control
I am trying to use InvokeScript method on the WebBrowser control to execute a script on the web page that has been loaded into my WebBrowser control. If I am calling just a simple javascript function, this works properly. However,开发者_如何学C the javascript function that I am trying to call is nested within a variable like this:
var holder =
{
DoA: function()
{
....
},
DoB: function()
{
.....
}
}
Calling holder.DoA works fine when called from within the javascript, but the function is not called successfully when I try to call it from within my C# code like this:
object obj1 = m_webBrowser.Document.InvokeScript("holder.DoA");
Any ideas?
I had the same problem and I got it to work like this:
m_webBrowser.Document.InvokeScript("eval", new object[] { "holder.DoA()" });
Have you tried window.holder.DoA as the parameter to InvokeScript ?
That's a singleton JSON class. If holder is a global variable, you can get its reference by calling IHTMLDocument::Script.
I am not sure whether Type.InvokeMember would work on a JSON object since the methods are added at runtime, try query IDispatchEx interface from the object to call the methods.
精彩评论