我正在使用visual studio c#2010作为Web浏览器.
WebBrowser 1导航到此链接:
当它到达页面时,它会加载并冻结.
我不认为网页有什么问题,因为chrome,firefox和常规IE9根本不会冻结.
只有当我导航到此链接时,我的c#程序中的Web浏览器才会冻结.
如何防止这种情况冻结?该网页似乎是从另一个网站调用一些HTML数据.
我尝试将此代码添加到我的程序中
this.webBrowser1.ScriptErrorsSuppressed = true;
我还更改了Web浏览器的注册表值,以便它将使用Internet Explorer版本9,到目前为止这两个都不起作用.
这是我正在使用的代码
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)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题不在于WebBrowser控件本身,而在于特定网站如何尝试执行一些陷入循环的Javascript.
比较和对比:
1)将网址更改为http://google.com.工作良好.
2)现在.为Navigating事件添加事件处理程序.就像是:
this.webBrowser1.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webBrowser1_Navigating);
Run Code Online (Sandbox Code Playgroud)
和
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
Console.WriteLine("Navigating to: " + e.Url);
}
Run Code Online (Sandbox Code Playgroud)
您将看到有一个JavaScript函数不断尝试重定向页面.这是我的控制台输出中显示的内容(无限期地继续):
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();})())
Run Code Online (Sandbox Code Playgroud)
这使得webBrowser控件基本上无法使用.
编辑: 好的,一个解决方法(这可能很糟糕,但令人沮丧的是,奇怪的重定向循环只发生在WebBrowser控件的浏览器中).
如果阻止导航事件在另一个导航事件完成之前被调用,它将加载页面并且不会冻结,并且链接似乎可以正常工作.它是这样的:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2471 次 |
| 最近记录: |