all groups > dotnet jscript > april 2005 >
You're in the

dotnet jscript

group:

Getting value of JavaScript variable in embedded WebBrowser Contro


Getting value of JavaScript variable in embedded WebBrowser Contro Randy
4/25/2005 6:09:02 AM
dotnet jscript:
Hi,

is it possible to programmatically (from C# or VisualBasic) get the value of
a JavaScript
Variable from a JavaScript running inside the embedded WebBrowser Control?

Thanks in advance, Randy
Re: Getting value of JavaScript variable in embedded WebBrowser Contro Bruce Barker
4/25/2005 9:45:54 AM
javascript global variables can be refered to as window.VarName

-- bruce (sqlwork.com)


[quoted text, click to view]

Re: Getting value of JavaScript variable in embedded WebBrowser Co Randy
4/26/2005 11:42:12 AM
ok - thanks... but how can I do this programmatically...

C#
webBrowser1.Document.... ? Could you please gimme a hint?

btw. is it possible to insert a code snipplet to a special location within
the html document, to modify or override for example the JavaScript Code with
my own code?

Thanks in advance, Randy



[quoted text, click to view]
Re: Getting value of JavaScript variable in embedded WebBrowser Co Serge Baltic
4/27/2005 9:25:12 AM
R> ok - thanks... but how can I do this programmatically...

You may use JScript.NET for that.

I find it to be the most convenient way. Make a DLL with the code on JScript.NET
that works with the browser, and call it whenever needed to say something
to the browser object. In that case webBrowser1.Document would be just the
HTML "document" object.

R> C#
R> webBrowser1.Document.... ? Could you please gimme a hint?

In C# it's not that convenient. Generally …

webBrowser1.Document.GetType().Invoke("methodname", …)

see help for System.Type.Invoke.

R> btw. is it possible to insert a code snipplet to a special location
R> within the html document, to modify or override for example the
R> JavaScript Code with my own code?

What do you want to do? Call your managed code from the browser script?

Two ways to do that, the first is to use the window.external which may point
to any object, including a C# one. Unfortunately, it's not easy to set the
external property to point to your object.

Another way is to assign a delegate to a property of some HTML element using
setAttribute. Then you may getAttribute from browser script and call it.
However, that must be a OLE delegate not .NET delegate. One can be provided
by JScript.NET, just set the attribute to one of your member functions, and
will be callable:

// in JS.NET
webBrowser1.Document.body.setAttribute("Callback", this.CallbackFunction);

// in browser script
var callback = document.body.getAttribute("Callback");
callback(param1, param2); // .NET method CallbackFunction will be called

However, that all is hardly needed — you may attach to an HTML element event
from managed JScript.NET code just the same way as from the browser script,
like:

document.getElementById("myButton").attachEvent("onclick", this.MyButtonClickHandler);

- or -

document.getElementById("myButton").onclick = this.MyButtonClickHandler;

From C#, all of the above is very hard to do.

(H) Serge

Re: Getting value of JavaScript variable in embedded WebBrowser Co Randy
5/3/2005 1:52:25 PM
Hi Serge,

thanks a lot for your reply! It helps me a lot understanding the DOM!

Greetings from Germany, Randy


[quoted text, click to view]
AddThis Social Bookmark Button