2

I'm using visual studio c# 2010 for the web browser.

WebBrowser 1 navigates to this link:

http://www.costco.com/IOGEAR-Wireless-1080p-HDMI-Transmitter-and-Receiver-3D-Compatible-2x-HDMI-Ports.product.100011675.html

When it reaches the page, it loads and freezes.

I don't think there is something wrong with the web page because chrome, firefox, and the regular IE9 don't freeze at all.

Only the web browser in my c# program freezes when it navigates to this link.

How do I prevent this from freezing? The web page seems to be calling some html data from another site.

I tried adding this code to my program

this.webBrowser1.ScriptErrorsSuppressed = true;

and I also changed the registry values of the web browser so that it will use internet explorer version 9 and so far these two did not work.

this is the code i'm using

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;

 namespace WindowsFormsApplication1
 {
     public partial class Form1 : Form
     {
         public Form1()
    {
        InitializeComponent();

        webBrowser1.ScriptErrorsSuppressed = true;
   }

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Navigate("http://www.costco.com/IOGEAR-Wireless-1080p-HDMI-Transmitter-and-Receiver-3D-Compatible-2x-HDMI-Ports.product.100011675.html");
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

         }
     }
 }
Kara
  • 6,115
  • 16
  • 50
  • 57
  • You might check whether the WebBrowser control is actually using the same version of IE that your Internet Explorer is. (Sounds weird, but bear with me.) Use each to visit a page that can detect browser version (e.g., http://www.whatismybrowser.com/). It could be that the webbrowser control is using an older version of IE... which you can alter in the registry. See here: http://stackoverflow.com/questions/5531452/webbrowser-control-to-use-ie9 and here: http://stackoverflow.com/questions/4612255/regarding-ie9-webbrowser-control – Gojira Aug 23 '13 at 17:03
  • I already did that, I changed the dword to 9999 and it shows that I'm using internet explorer 9.. could you please recreate my program in your computer using the code above? I believe you're going to get the same result – Afalfjd Aslfkjasd Aug 23 '13 at 17:23

1 Answers1

5

The issue is not with the WebBrowser control per se, it is with how that particular website is trying to execute some Javascript that gets stuck in a loop.

Compare and contrast:

1) Change the url to http://google.com. Works fine.

2) Now. Add an event handler for the Navigating event. Something like:

this.webBrowser1.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webBrowser1_Navigating);

and

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    Console.WriteLine("Navigating to: " + e.Url);
}

You will see that there is a JavaScript function that is constantly trying to redirect the page. Here's what shows up in my console output (goes on indefinitely):

Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})())
Navigating to: about:blank
Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})())
Navigating to: about:blank
Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})())
Navigating to: about:blank
Navigating to: javascript:void((function(){document.open();document.domain='costco.com';document.write('<!DOCTYPE html>');document.close();})())

Which makes the webBrowser control essentially unusable.

EDIT: Ok, one stab at a workaround (this is probably terrible, but it's frustrating that the weird redirect loop is only happening in the WebBrowser control's browser).

If you block the Navigating event from being called before another Navigating event has completed, it loads the page and does not freeze, and the links appear to work. It goes something like this:

 private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        Console.WriteLine("Navigated to: " + e.Url);
        isNavigating = false;
        webBrowser1.AllowNavigation = true;
    }

    bool isNavigating = false;
    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        if (isNavigating && e.Url.ToString().Contains("javascript:void((function(){document.open();document.domain='costco.com'"))
        {
            webBrowser1.Stop();
            webBrowser1.AllowNavigation = false;
            return;
        }

        isNavigating = true;
        Console.WriteLine("Navigating to: " + e.Url);
    }
Gojira
  • 2,941
  • 2
  • 21
  • 30
  • This is actually a very excellent way to prevent it from freezing. However, when you stop the web browser, there seems to be other data that needs to be downloaded but weren't downloaded. I was wondering if there is a way to ignore the navigation request when it leads to "javascript:void((function(){document.open();document.domain='costco.com" – Afalfjd Aslfkjasd Aug 25 '13 at 03:50
  • Yes, if you call the webBrowser's Stop() method in the IsNavigating event, it will cancel the navigation. However, I think in this case it will cause the page not to load that page correctly if you simply stop that JavaScript call. I tried a few combinations of stopping the navigation to about:blank or the JavaScript function you mentioned; the best luck I had in the end was with the code above. – Gojira Aug 25 '13 at 13:01