C# Webbrowser and client side Javascript

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.
 
G

Geoffrey Summerhayes

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.
 
A

Andrew

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
 
A

API

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();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top