Webbrowser Control and Javascript

Posted on 2006-07-24
Last Modified: 2013-12-25
Hello guys,

What i want to do is to call a vb function when javascript alert function is run from the webbrowser window. So that no alertbox will appear on the screen and page will continue running... I've tried several ways before, one of them is to overwrite the existing javascript function, but it doesnt prevent the alerts when the page is loading... etc...

in short here is what i want;
when called from the browser window

alert("Hello") must call my alerthandle function in vb with "Hello" as parameter

if its not possible then i am looking for a way to disable alert boxes...


Question by:cem_turk
  • 2
LVL 17

Accepted Solution

vb_elmar earned 500 total points
ID: 17175414

JavaScript Messageboxes (e.g. alert"Hello") are modal windows, and a standard VB Timer1 timer stops, when a
modal JavaScript Messagebox pops up.

So, a normal VB timer would not be helpful.

To detect Javascript alert dialog boxes, you need a subclassing timer
that doesn't stop, when a VB MsgBox or a JS MsgBox pops up.

The following sample works with a subclassing Timer.

How the sample works :

1)   It detects the alertbox (using findwindow).
2)   If found, it sends a sendkeys"{Enter}".
---> Then the alertbox will disappear

1 Form with "WebBrowser1" (press Ctrl-T   --->  "Microsoft Internet Controls")
1 Module
1 Class module (name: TimerInterface)

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetTimer& Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Private Declare Function KillTimer& Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long)

Dim cnt%
Private TIS As TimerInterface 'declare Interface
Implements TimerInterface 'and Implement interface to form

'**********************REQUIRED BY DELEGATON PER METHOD!************************
Private ObjectProcedures() As FunctionSPointerS '<------Class Procedure pointers and addresses!
Private HMEM As Long   '<--------------Memory Handle
Private TIMERPROC As Long  '<---------AddressOf Delegator (who call our Class Method!)
Private HTIMER As Long '<--------Timer Handle

Private Sub Form_Activate()
    WindowState = 2: BackColor = RGB(166, 166, 144): AutoRedraw = True
    Me.Caption = ""
    WebBrowser1.Move 0, 0, Screen.Width / 2, Screen.Height * 0.9: DoEvents
    WebBrowser1.Navigate "C:\WINDOWS\Desktop\Msgbox.HTM"
    a = WebBrowser1.ReadyState
    If a <> myState Then myState = a: Me.Caption = Me.Caption & "      readystate-->:" & a
    Loop While a <> myState Or a <> 4
    Caption = Caption & "      ***READY***"
    Set o = WebBrowser1.Document
End Sub

Private Sub Form_Load()
'REQUIRED! Set Interface Procedure Pointer on TIS to Our Form Implemented Interface!!!
Set TIS = Me

'REQUIRED BY DELEGATION PER METHOD!**********************************************
ObjectProcedures = GetObjectFunctionsPointers(TIS, 1)
HMEM = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, CalculateSpaceForDelegation(4))
TIMERPROC = GlobalLock(HMEM) 'Get Address
DelegateFunction TIMERPROC, TIS, ObjectProcedures(0).FunctionAddress, 4
'USE DELEGATION For 1st Method In SubclassingInterface Class!!! -->ObjectProcedures(0)

'Set Timer!
HTIMER = SetTimer(Me.hwnd, 0, 0, TIMERPROC) '<<---Call ASM Code!

End Sub

Private Sub Form_Unload(Cancel As Integer)
'Kill Timer!
KillTimer Me.hwnd, HTIMER

'Free Memory Allocated For Delegation
Call GlobalUnlock(HMEM)
Call GlobalFree(HMEM)
End Sub

Private Sub TimerInterface_TIMERPROCEDURE(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    If cnt = 0 Then
    Caption = "dwTime: " & dwTime
        Dim fw&
        fw = FindWindow("#32770", "Microsoft Internet Explorer")
        If fw Then SendKeys "{Enter}"
    End If
    cnt = IIf(cnt = 14, 0, cnt + 1)
End Sub

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare Function GlobalFree Lib "kernel32.dll" (ByVal HMEM As Long) As Long
Declare Function GlobalLock Lib "kernel32.dll" (ByVal HMEM As Long) As Long
Declare Function GlobalUnlock Lib "kernel32.dll" (ByVal HMEM As Long) As Long

Public Const GMEM_MOVEABLE As Long = &H2
Public Const GMEM_ZEROINIT As Long = &H40

Public Type FunctionSPointerS
FunctionPtr As Long
FunctionAddress As Long
End Type

Public Function CalculateSpaceForDelegation(ByVal NumberOfParameters As Byte) As Long
CalculateSpaceForDelegation = 31 + NumberOfParameters * 3
End Function

Public Function DelegateFunction(ByVal CallingADR As Long, Obj As Object, ByVal MethodAddress As Long, ByVal NumberOfParameters As Byte) As Boolean
On Error GoTo NotSuccess
Dim TmpA As Long
TmpA = CallingADR
CopyMemory ByVal CallingADR, &H68EC8B55, 4
CallingADR = CallingADR + 4
CopyMemory ByVal CallingADR, TmpA + 31 + (NumberOfParameters * 3) - 4, 4
CallingADR = CallingADR + 4

Dim StackP As Byte
StackP = 4 + 4 * NumberOfParameters

For u = 1 To NumberOfParameters
CopyMemory ByVal CallingADR, CInt(&H75FF), 2
CallingADR = CallingADR + 2
CopyMemory ByVal CallingADR, StackP, 1
CallingADR = CallingADR + 1
StackP = StackP - 4
Next u

CopyMemory ByVal CallingADR, CByte(&H68), 1
CallingADR = CallingADR + 1
CopyMemory ByVal CallingADR, ObjPtr(Obj), 4
CallingADR = CallingADR + 4
CopyMemory ByVal CallingADR, CByte(&HE8), 1
CallingADR = CallingADR + 1
PERFCALL = CallingADR - TmpA - 1
PERFCALL = MethodAddress - (TmpA + (CallingADR - TmpA - 1)) - 5
CopyMemory ByVal CallingADR, PERFCALL, 4
CallingADR = CallingADR + 4
CopyMemory ByVal CallingADR, CByte(&HA1), 1
CallingADR = CallingADR + 1
CopyMemory ByVal CallingADR, TmpA + 31 + (NumberOfParameters * 3) - 4, 4
CallingADR = CallingADR + 4
CopyMemory ByVal CallingADR, CInt(&HC2C9), 2

CallingADR = CallingADR + 2
CopyMemory ByVal CallingADR, CInt(NumberOfParameters * 4), 2



'*********** Depend on Number of Parameters
'  .......

'RET 00XX Depend on Number of Parameters

'Thats IT! Nothing less than 39 BYTES Of ASM Code!

DelegateFunction = True
Exit Function
On Error GoTo 0
End Function
Public Function GetObjectFunctionsPointers(Obj As Object, ByVal NumberOfMethods As Long, Optional ByVal PublicVarNumber As Long, Optional ByVal PublicObjVariantNumber As Long) As FunctionSPointerS()
Dim FPS() As FunctionSPointerS
ReDim FPS(NumberOfMethods - 1)
Dim OBJ1 As Long
OBJ1 = ObjPtr(Obj)
Dim VTable As Long
CopyMemory VTable, ByVal OBJ1, 4
Dim PTX As Long
Dim u As Long
For u = 0 To NumberOfMethods - 1
PTX = VTable + 28 + (PublicVarNumber * 2 * 4) + (PublicObjVariantNumber * 3 * 4) + u * 4
CopyMemory FPS(u).FunctionPtr, PTX, 4
CopyMemory FPS(u).FunctionAddress, ByVal PTX, 4
Next u
GetObjectFunctionsPointers = FPS
End Function

Class module (name: TimerInterface)
Public Sub TIMERPROCEDURE(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
End Sub

LVL 17

Expert Comment

ID: 17175420
File "C:\WINDOWS\Desktop\Msgbox.HTM" :

<script type="text/javascript">
function msgbox () {
  alert('Hello World');

<body id="HauptBody" onload="javascript:msgbox()" bgcolor="#00FFFF">

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now