首先在工程——引用中引用 Microsoft HTML Object Library
Option Explicit
Private WithEvents m_MyVar As HTMLInputElement
Private Sub Form_Load()
WebBrowser1.Navigate2 "http://www.xyserver.com"
End Sub
'讲拦截后的信息由window对话框显示
Private Function m_MyVar_onchange() As Boolean
MsgBox "Alert的内容是: " & m_MyVar.Value
End Function
Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object,URL As Variant)
Dim oWindow As HTMLWindow2
Dim oDoc As HTMLDocument
Dim doc
Set oDoc = pDisp.Document
Set oWindow = oDoc.parentWindow
Set m_MyVar = oDoc.createElement("input")
m_MyVar.Type = "Hidden"
m_MyVar.id = "MyVar"
'------------------------------------------------------------
'阻止弹出对话框
Set doc = WebBrowser1.Document
doc.parentWindow.execScript "function alert(){return;}"
doc.parentWindow.execScript "function confirm(){return;}"
doc.parentWindow.execScript "function showModalDialog(){return;}"
'-------------------------------------------------------------
oDoc.getElementsByTagName("HEAD").Item(0).appendChild m_MyVar
oWindow.execScript "var oldalert=window.alert;window.alert=function myalert(msg){oldalert(msg);MyVar.value=msg;MyVar.fireEvent(""onchange"");};"
End Sub
'拦截弹出来的Web窗口 Private Sub WebBrowser1_NewWindow2(ppDisp As Object,Cancel As Boolean) 'Set ppDisp = WebPop(0).Object Cancel = True End Sub