Click here to Skip to main content
15,867,594 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:
HTML
<div id="div1" >
</div>
<div id="div2">
</div>


Javascript:
JavaScript
function func1(element) {
    ...
}

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


C# code in my windows form application after navigating the related URL:
C#
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.
Posted
Updated 13-Oct-17 7:56am
v4

1 solution

For el1:

C#
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.
 
Share this answer
 

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