Click here to Skip to main content
15,178,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm developing a windows form application to parse and analyze web pages. I used webbrowser control. The web page has html and javascript code bellow. and my windows form has c# code. Here is the code:

HTML code:
<div id="div1" >
<div id="div2">

function func1(element) {

document.getElementById("div2").onclick = function foo() { doSomething(); }

C# code in my windows form application after navigating the related URL:
HtmlElement el1 = WebBrowser.Document.GetElementById("div1");
HtmlElement el2 = WebBrowser.Document.GetElementById("div2");
String el1JavaFunc = el1.GetAttribute("onClick");
String el2JavaFunc = el2.GetAttribute("onClick");

The result for both el1JavaFunc and el2JavaFunc is System.__ComObject.

How could I get the assigned javascript function name? For instance get func1 for el1.

Should I use any other control instead of WebBrowser?

Please Note that I don't need to execute the events and I know that System.__ComObject is like a wrapper for JavaScript functions.
Updated 13-Oct-17 8:56am

1 solution

For el1:

mshtml.IHTMLDOMNode node = el1.DomElement as mshtml.IHTMLDOMNode;
String el1JavaFunc = node.attributes("onclick").value.ToString();

And the same can be done for el2. Except I don't think it will return func1; I don't see why you expect that.

See System.__ComObject is returned when I use getAttribute.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900