Can we talk architecture first?
My I ask why webbrowser control? We have stopped using it for exactly this kind of reasons. It is usually more pain than gain.
It is also (and usually) product of an tightly coupled architecture.
For local html pages (aka clients) we use hta. If (and that does not happen often) we need to have C# code in the "back" we use our simple C#/COM+ enabled local server. (note: this is simpler than HttpListener based local server ).
Second: implementation issues.
Imagine you are a developer of Document.InvokeScript();
. What would you return from it?
What type? What you get from the HTML Document is DOM. Script is kind of a DOM node.
And javascript/DOM is implemented as "com object" indeed. which has IDispatch interface, which in turn returns VARIANT's to the host (aka browser). For a simple reason that javascript has no types in the C and COM meaning. The bottom line is: javascript is seen by COM as returning (and receiving) javascript Object's only. What actually is inside is implementation dependent. Therefore it would be extremely difficult to guess what is inside IDispatch instance returned by javascript and to parse this into the COM types.
This is left to the users/callers.
So. It is much easier to establish strings as the only type used to communicate with the "back" from javascript. And to structure these strings as either xml or json. Exactly the same as communicating with distant web services, we communicate with the local C#/COM+ server.
// HTA, non Ajax solution
var local_server = new ActiveXObject("Stevo3000.LocalServer");
local_server.send(
"{ method: 'getname', id: 123 }" ,
function ( data )
{
alert("Stevo says:" + data ) ;
}
) ;
NOTE: Since we are talking here C#, we know that all of this is happening on the WIN32 desktop, so we use MSFT tools and technologies. Exactly the same principle can be applied for cross browser, platfom agnostic solution, by using AJAX only.
// Pure Ajax
// using jQuery
$.post("http://stevo3000.com/nameprovider.aspx",
{ method: "getname", id: "123" },
function(data){
alert("Stevo says:" + data ) ;
});
Hope this helps?
--DBJ