C# Webbrowser and client side Javascript

  • Thread starter Thread starter andrewb
  • Start date Start date
A

andrewb

I have a C# .NET 2005 application that uses the WebBrowser control to
render a client-side webpage that uses javascript.

The C# application needs to be able to access the values of javascript
variables (including arrays) on the page.

I have used the following references but neither seems to offer me a
way of getting to the javascript variables ...

mshtml.IHTMLDocument2 doc = wb.Document as mshtml.IHTMLDocument2;
IHTMLWindow2 parent = doc.parentWindow as mshtml.IHTMLWindow2;

Have scoured the web for examples but with no success - any help you
could offer would be much appreciated.
 
I have a C# .NET 2005 application that uses the WebBrowser control to
render a client-side webpage that uses javascript.

The C# application needs to be able to access the values of javascript
variables (including arrays) on the page.

I have used the following references but neither seems to offer me a
way of getting to the javascript variables ...

mshtml.IHTMLDocument2 doc = wb.Document as mshtml.IHTMLDocument2;
IHTMLWindow2 parent = doc.parentWindow as mshtml.IHTMLWindow2;

Have scoured the web for examples but with no success - any help you
could offer would be much appreciated.

Can't do it. The javascript is running in the browser and it's state
isn't
captured and sent to the server. Aside from bandwidth issues, what
would be the point? You might as well run all the code on the server.

That being said, you can pass information in hidden elements in a
form,
or you can use something like ajax to call a service and pass the
information.
 
Hi Geoff,

Thanks for your reply.

Sorry if I wasnt clear but the C# application is rendering a local HTML page
on the client so it should be possible to get to it ...

Thanks,

A
 
Andrew said:
Hi Geoff,

Thanks for your reply.

Sorry if I wasnt clear but the C# application is rendering a local HTML page
on the client so it should be possible to get to it ...

Thanks,

A

Hi Andrew,

Try exec this script

GetVarScript1 = "if(document.getElementById('hiddenVarValue')==null)
{
var elem = document.createElement('INPUT');
elem.setAttribute('id','hiddenVarValue');
elem.setAttribute('type','hidden');
document.body.appendChild(elem);
}
document.getElementById('hiddenVarValue').setAttribute('value',chatMsgId);";

where chatMsgId is the variable name.
Then get value of element 'hiddenVarValue' with IHTMLDocument2 interface
using code like this:

wnd->execScript(GetVarScript1,"JScript");
varIndex = "hiddenVarValue";
IHTMLElementPtr Var1 = doc1->all->item(&varIndex);
varIndex.Clear();
varIndex = Var1->getAttribute("value",0);
MsgIDText.SetWindowText((_bstr_t)varIndex);
varIndex.Clear();
 
Back
Top