Link to home
Start Free TrialLog in
Avatar of NessieB
NessieBFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Webbrowser question - Stop Java script error message pop ups ?

I use a webbrowser object and occasionally I get a 'Javascript' error page message pop up

"internet explorer script error
an error has occured in thescript on this page
Do you wish to continue to running scripts on this page"

WebBrowser2.Silent = True 'stops  all pop ups even the ones I need like login promps etc

Is there a way of blocking ONLY the java scipt message but leaving the useful msgbox promps like "you are being redirected to a secure server"  etc etc ?
Avatar of Erick37
Erick37
Flag of United States of America image

There is an option in IE:

Tools->Internet Options...->[Advanced]

Find the option "Display a notification about every script error"
Avatar of edwardiii
edwardiii

Hi, Nessie.

If the above doesn't solve the issue for you, take a look at my post here (https://www.experts-exchange.com/questions/21372338/Webbrowser-Stopping-process-on-Error-Message.html).  Basically you can determine the caption name of the pop-up you wish to prevent and programmatically close it when it does pop up:

1)  Add the following code to your form's General Declarations section:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

     Private Const SW_SHOW = 5
     Const WM_CLOSE = &H10


2)  Next, initialize the following variables in your form's Form Load event:

    Dim winHwnd As Long
    Dim RetVal As Long
    Dim strPopupName As String


3)  Then, in your Timer routine, add:

     'Private Sub Timer1_Timer()
   
          strPopupName = "Internet Explorer"
          winHwnd = FindWindow(vbNullString, strPopupName)
          'Msgbox winHwnd
         
          If winHwnd <> 0 Then
              PostMessage winHwnd, WM_CLOSE, 0&, 0&
          End If

     'End Sub

Note:  I've got this set up with strPopupName = "Internet Explorer".  You may need to alter the text in quotes.  It needs to exactly match the title of your error message's pop-up box.  To see if the handle of the pop-up is being found, you can enable the "Msgbox WinHwnd" above--if it displays "0", then title needs to be adjusted for the "strPopupName = ..." statement.
not sure if this works in VB... but I believe there is a Silent property for the webbrowser control... setting this to true should get rid of any prompts that a webpage would cause... im not sure if this is only for permission asking, or for javascript errors aswell
axwebbrowser1.Silent = True
sorry... i didnt read the question at all, ignore me, *hangs head in shame*
I suppose an alternative (after reading the question properly... sorry again... it's late here).. would be to put silent on... but to trap the events of the WebBrowser control to get the "useful" information you require... without knowing what you deem useful, would be a bit hard to post up code... for instance though, being redirected to a secure server, you could check the navigate events and check the url (or maybe there is a better way of checking how secure the current page is and monitoring a change)
Avatar of NessieB

ASKER

Edwardiii

The caption title is "Internet Explorer Script Error"

I set strPopupName = "Internet Explorer Script Error"

But  I get   "Msgbox winHwnd = 0 "   

so how do I get the caption title?
Avatar of NessieB

ASKER

No response Guys? Can it not be done?
Did you disable the "Display a notification about every script error" option?
Hi, Nessie.  Sorry--was out of town and away from email.  Can you give me a URL where this occurs so I can tinker with the code I provided earlier?
Avatar of NessieB

ASKER

can you explain "Did you disable the "Display a notification about every script error" option?"

edward can I have code that is not URL dependant?

I only want to bock the pop ups that display that "Internet Explorer Script Error" - can it be done?
If you read up... Erick37 pointed out an option that is within Internet Options (within Control Panel... or from the IE Tool Menu)
Right, well that does not work, as it turns out :/

You can supress the JS errors in IE, but using the Webbrowser control in VB6 it becomes extremely difficult.

You can do it in C++ and in .NET by implementing the IOleCommandTarget interface and catching the errors.  To do this in VB6 you would need to write a C++ wrapper control to do this directly (I think)

See:
How to handle script errors as a WebBrowser control host
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B261003

So, the next best thing is perhaps seek and destroy the popups when they happen as suggested by edwardiii.
Yes, the code we're working on needs no URL--I just asked for it so I could see exactly what you're seeing.  Barring that--did you try setting strPopupName = "Internet Explorer" rather than "Internet Explorer Script Error"?  Sometimes Windows truncates the name.  For example, the literal caption/heading of the IE page might be "Howdy - Java Usage", but Windows will set strPopupName = "Howdy".

Avatar of NessieB

ASKER

--- OR is there a way of actually getting the caption of the window with an API ?
This code:

     winHwnd = FindWindow(vbNullString, strPopupName)

Finds the "handle" (ID number Windows assigns to objects that are running).  If you know the type of the parent/super object (the "vbNullString" above, and you knew your were searching for a Notepad object, you'd replace vbNullString with Notepad), and search for that.  The problem is there might be multiples of that object (for example, multiple Notepads open).  In that case, knowing the object type "Notepad" wouldn't be enough--you'd have to know the name also. So we're searching by the name "strPopupName" Windows is assigning your object, rather than it's structural name (e.g. Untitled - Notepad, versus Notepad).

Maybe it would be best if we have you run an enumeration on all the objects open on your desktop.  You can hunt through the results and that way we can get the exact name of the window in question (you'll need to run the enumeration when the pop-up is visible).

Give me a little bit to put together a routine, and I'll post back shortly.
ASKER CERTIFIED SOLUTION
Avatar of edwardiii
edwardiii

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NessieB

ASKER

Thanks v.much for all your efforts- very much appreciated. I will look at this later and get back. Thanks again.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NessieB

ASKER

edward I ran your code but nothing happened as far as I could see. Where should I look for  "every window handle/title caption" is it a list? a msgbox?
The info is being displayed in your Immediate Window.  In VB, go to the top menu and View/Immediate Window.  When you run the program, a listing will appear in that window.
Avatar of NessieB

ASKER

Do the fact that I am using a webbrowser control make a difference?
Make a difference...as far as:

1)     getting results in the Immediate Window--no.  By the way, did you get a listing of all open windows on your desktop, including the pop-up window?  Can you list the results here?  Please close every application running on your desktop except Visual Basic--that will make this much easier.

2)     the ability to shut down an open window spawned by Web page interactions--no.  Whether you get the pop-up from an independent IE window or from the WebBrowser control, the pop-up window created is a unique window within your OS environment.  You do see the pop-up in your taskbar, right?
Avatar of NessieB

ASKER

Im afraid your solutions dont work for me. I ended up doing this. I will no doubt find a correct answer later. I have split the points for your hard work.

Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
    stxta = "https://www.badUrl.com"
    stxtb = "http://www.goodUrl"

    If Mid(URL, 1, Len(stxta)) = stxta Then
    WebBrowser1.Silent = False
    End If

    If Mid(URL, 1, Len(stxtb)) = stxtb Then
    WebBrowser1.Silent = True
    End If
End Sub
Thanks for the points:)  I'm wondering, what were the results of the enumeration routine?  Even if we're dealing with the wrong window name, you can take each window handle and run it through a closing loop routine (of course omitting handles of applications you want to keep open).  The pop-up's handle has got to be listed in the enumeration (presuming it is visible when you run the enumeration)...
Thanks to PaulHews for this important Timer fact (from EE post: https://www.experts-exchange.com/questions/21473851/Error-handling-web-page-errors.html):

     "Timers will stop firing in the IDE with a modal message box, but not in a compiled application." This may explain why the Timer-based code above didn't work (e.g. it was being tested in the IDE).
Yep - that's what I said in the post above:

"Please note - this code will *not* work when run in the IDE.  You must compile the code and run the EXE file to see it in action.  Why?  Because the timer control does not fire when a modal dialog box is displayed when run from the IDE.  Once compiled, the timer event will fire even if a modal dialog box is pending."
Yikes! Sorry for missing that in your comments, Erick37. Thanks for the clarification:)