如何忽略Webbrowser中的脚本错误

时间:2011-01-11 08:38:38

标签: c# browser

当我关闭使用webbrowser的应用程序时会发生什么。以下网址使用flash播放器。

alt text

一切正常。应用关闭时显示此错误。我怎么忽略它?

5 个答案:

答案 0 :(得分:2)

您可以通过将WebBrowser控件的ScriptErrorsSuppressed属性设置为true来禁用javascript错误。但有时它可能不起作用。如果它不起作用,请检查http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/46a32b08-3834-4a13-8170-e0eba2498284http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/07df5263-613c-4780-89a2-67ebf2a1e670

答案 1 :(得分:2)

我知道为时已晚,但我觉得我对这个问题有一个明智的答案。

使用它,它可以在飞行中为我工作。 :)

webBrowser.ScriptErrorsSuppressed = true;

如果它不起作用,我们可以使用不同的方法,例如显示确认框(例如:这个窗口想要关闭你想继续是/否)

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

 private void TimerPopUp_Tick(object sender, EventArgs e)
        {
            IntPtr hWnd1 = FindWindowByCaption(IntPtr.Zero, "Web Browser");

            if (hWnd1 != IntPtr.Zero && SetForegroundWindow(hWnd1))
            {
                SendKeys.Send("{Enter}");
            }

        }

如果有任何错误,请参阅此link

答案 2 :(得分:0)

首先,您需要知道webbrowser控件使用IE7作为其基础(即旧版本的Internet Explorer),因此您现在运行的脚本与现代浏览器兼容,从而与错误兼容。 首先,如果你把:

<meta http-equiv="X-UA-Compatible" content="IE=edge"/>

在你的页面部分内,它将使用机器上安装的IE版本而不是默认的IE 7进行渲染

希望这有帮助!

答案 3 :(得分:0)

您应该使WebBrowser Control COM可见并为其提供适当的权限:

engine.eval("var webPage = require('webpage');
                                              ^
        + "var page = webPage.create();"

或:

#region Using Statements:



using System;
using System.Windows.Forms;
using System.Security.Permissions;



#endregion



[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{



public Form1()
{

InitializeComponent();

// WebBrowser Configuration:
webBrowser1.ObjectForScripting = this;
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.IsWebBrowserContextMenuEnabled = false;

}




private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.com/");
}


}

注意:这可能无法在调试模式下正常运行。

此外,如果您需要为本地应用程序兼容性设置注册表项,它也不会在调试模式下运行。

#region Using Statements:



using System;
using System.Windows.Forms;
using System.Security.Permissions;



#endregion



[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{



public Form1()
{

InitializeComponent();

// WebBrowser Configuration:
webBrowser1.ObjectForScripting = new ObjectForScripting();
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.IsWebBrowserContextMenuEnabled = false;

}




private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.com/");
}


[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class ObjectForScripting
{

// User Code to handle events..

}


}

答案 4 :(得分:0)

Visual Studio 2015中,有一个选项可以启用或禁用弹出窗口。

web browser propertiesScript Errors Suppressed中,然后将其设置为True以禁用弹出窗口。

相关问题