Dobermann
Dobermann

Reputation: 9

Calling JavaScript in a WebBrowser control from C#

For example: 1) In webBrowser1 page index.html is loaded. 2) This page has the following code:

...
<a id="activity_text" href="#" onclick="activity_editor.show();return false;">now status</a>
...

3) As I can in the program way to change "now status"?

I tried so:

HtmlElement collH1 = document.GetElementById("activity_text");
collH1.InnerText = "new status";

But this way works only in the control webBrowser1. If then to come to look through IE/Opera/FF that has varied of nothing...

Upvotes: 1

Views: 420

Answers (1)

Mikael Svenson
Mikael Svenson

Reputation: 39697

If you are using System.Windows.Forms.WebBrowser:

HtmlElement collH1 = document.GetElementById("activity_text");
object obj = collH1.DomElement;
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
mi.Invoke(obj, new object[0]);

If you're using mshtml:

HtmlElement collH1 = document.GetElementById("activity_text");
mshtml.HTMLAnchorElement el2 = (mshtml. HTMLAnchorElement)collH1.DomElement;
el2.click();

This will execute a click similar to a user clicking the link in a browser, which I guess is what you want to accomplish(?).

Upvotes: 1

Related Questions