0

I am trying to write a 'win app' that is connecting to a web page. you suppose this is the html page

<a href="javascript:fun(p)">click me</a>
<a href="javascript:go('N')">Next</a>

I want to load this page on a WebBrowser control and find hyperlinks within that. after that add them into a ListView control. I am ok by first links but I have problem with navigating to the next page.

this is my c# code :

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{

    HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("a");
    int i = 1;
    foreach (HtmlElement item in col)
    {
        if (item.GetAttribute("href").Contains("go"))
        {
            object[] o = new object[1];
            o[0] = "N";
            object res = this.webBrowser1.Document.InvokeScript("go", o);

        }
        else
        {
            ListViewItem lvi = listView1.Items.Add(item.InnerText);
            lvi.SubItems.Add(item.GetAttribute("href"));
            toolStripStatusLabel1.Text = i.ToString();
            i++;
         }

    }

}

I get this error

enter image description here

and it doesn't invoke javascript function and I can't go to the next page by calling javascript function.

I guess it is because of WebBrowser. I think The WebBrowser doesn't load completely.

Now I am completely confused how can I fix the problem. I will appreciate any help or suggestion and finally I am sorry for my poor English.

gilly3
  • 87,962
  • 25
  • 144
  • 176
Kia Boluki
  • 315
  • 3
  • 15
  • "whats wrong in invoking javascript function in c#" --- everything is wrong. One works on a server, another on a client. – zerkms Jun 03 '14 at 21:39
  • [How to call JavaScript from HREF](http://stackoverflow.com/questions/16337937/how-to-call-javascript-from-a-href) – MethodMan Jun 03 '14 at 21:47
  • 2
    It seems like the error is not in your code, but in the script of the page you load. To test your code, try it with a simple web page you create, with a simple function, like alert. That js error message from WebBrowser's IE doesn't seem related at all to your code. – Rudy Jun 03 '14 at 21:53
  • @zerkms - Both JavaScript and C# are perfectly fine languages to use in either client or server environments. – gilly3 Jun 03 '14 at 22:11
  • @gilly3: have you ever heard of the "context" term? In the *context of the question* - one runs on a server, another runs on a client. – zerkms Jun 03 '14 at 22:40
  • @zerkms - The C# in this question is a WinForms app, which runs client side. The JavaScript in this question is browser based, which is also client side. The question is about interacting with a web page within a web browser control in a WinForms app. Sure, there is a server involved somewhere (where did the currently loaded page come from), but that aspect is irrelevant to the question. Besides, the error message indicates the browser has loaded an asp page, which would be written in either JavaScript or VBScript. – gilly3 Jun 03 '14 at 23:27
  • @gilly3: do you realize c# and javascript run in different environments? – zerkms Jun 03 '14 at 23:44

0 Answers0