VB.NET让webbrowser控件中JS脚本错误最新方法(2013-09-16)
最近也是在项目中遇到了webbrowser控件中想关闭JS脚本错误窗口的问题,所以经过多次测试,终于用一段高效实用的代码完美解决webbrowser控件中JS脚本错误窗口关闭的问题。
通过创建一个子线程,然后在子线程中不断的去查找各类webbrowser的弹出窗口(alert、JS错误窗口),然后通过sendmessage函数来关闭窗口实现该功能!
以下代码可以解决webbrowser控件中的JS脚本错误窗口、alert窗口等各种浏览器弹出的窗口,能自动关闭
VB.NET代码如下:
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, _ ByVal wparam As Integer, ByVal lparam As IntPtr) As IntPtrDeclare Auto Function FindWindowEx Lib "user32.dll" (ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, _ ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtrPublic Const WM_CLOSE = &H10Private Sub threadCheckError() Dim hwnd As IntPtr While 1 hwnd = FindWindowEx(0, 0, "Internet Explorer_TridentDlgFrame", "Internet Explorer 脚本错误") If hwnd.ToInt64 > 0 Then SendMessage(hwnd, WM_CLOSE, 0, 0) End If hwnd = FindWindowEx(0, 0, "#32770", "来自网页的消息") If hwnd.ToInt64 > 0 Then SendMessage(hwnd, WM_CLOSE, 0, 0) End If System.Threading.Thread.Sleep(100) My.Application.DoEvents() End WhileEnd SubDim threadchk As New Threading.Thread(AddressOf threadCheckError)Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load WebBrowser1.ScriptErrorsSuppressed = False threadchk.Start()End sub