?
Solved

Webbrowser question - Stop Java script error message pop ups ?

Posted on 2005-04-07
28
Medium Priority
?
2,506 Views
Last Modified: 2010-08-05
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 ?
0
Comment
Question by:NessieB
  • 10
  • 8
  • 5
  • +1
28 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 13731363
There is an option in IE:

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

Find the option "Display a notification about every script error"
0
 
LVL 10

Expert Comment

by:edwardiii
ID: 13731472
Hi, Nessie.

If the above doesn't solve the issue for you, take a look at my post here (http://www.experts-exchange.com/Q_21372338.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.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13731820
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:S-Twilley
ID: 13731823
axwebbrowser1.Silent = True
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13731827
sorry... i didnt read the question at all, ignore me, *hangs head in shame*
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13731842
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)
0
 

Author Comment

by:NessieB
ID: 13734223
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?
0
 

Author Comment

by:NessieB
ID: 13738005
No response Guys? Can it not be done?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13738046
Did you disable the "Display a notification about every script error" option?
0
 
LVL 10

Expert Comment

by:edwardiii
ID: 13738229
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?
0
 

Author Comment

by:NessieB
ID: 13738409
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?
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13739559
If you read up... Erick37 pointed out an option that is within Internet Options (within Control Panel... or from the IE Tool Menu)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13739714
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.
0
 
LVL 10

Expert Comment

by:edwardiii
ID: 13739879
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".

0
 

Author Comment

by:NessieB
ID: 13739929
--- OR is there a way of actually getting the caption of the window with an API ?
0
 
LVL 10

Expert Comment

by:edwardiii
ID: 13740049
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.
0
 
LVL 10

Accepted Solution

by:
edwardiii earned 1200 total points
ID: 13740819
Okay, for now REM out the APIs we put in your General Declarations section.  Add a new command button with this code:

     EnumWindows AddressOf EnumWindowsProc, ByVal 0&

Add a module to your project and drop this code into it:

     Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
     Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
     Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean

    Dim windowCaption As String, Ret As Long
   
    Ret = GetWindowTextLength(hwnd)
    windowCaption = Space(Ret)
    GetWindowText hwnd, windowCaption, Ret + 1
       
    If Not InStr(windowCaption, "DoesNotExist") Then
        Debug.Print hwnd, windowClass, windowCaption
    End If
        EnumWindowsProc = True
End Function

Turn on your Immediate window, run the program, and click the command button (while the "Internet Explorer Script Error" pop-up is visible).  This enumeration will list every window handle/title caption running on your desktop.  Look in the immediate window.  Do you see anything like "Internet Explorer Script Error"?  
0
 

Author Comment

by:NessieB
ID: 13742909
Thanks v.much for all your efforts- very much appreciated. I will look at this later and get back. Thanks again.
0
 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 300 total points
ID: 13745617
More code to peruse.  This example uses a Timer control to search for the IE popup window class "Internet Explorer_TridentDlgFrame" while the page is loading.  It sends an {ENTER} if one is found.

This will take care of the standard Script Error messages.  It isn't a pretty solution but it seems to work.

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.

Enjoy!

Option Explicit

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

Private Sub Command1_Click()
    WebBrowser1.Navigate "http://www.mcli.dist.maricopa.edu/tut/tut27a_ex/test.html"
End Sub

Private Sub Form_Load()
    Timer1.Enabled = False
    Timer1.Interval = 100
    WebBrowser1.Navigate2 "about:blank"
End Sub

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)
    Timer1.Enabled = True
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    If pDisp Is WebBrowser1.Object Then
        Timer1.Enabled = False
    End If
End Sub

Private Sub Timer1_Timer()
    Dim lhWnd As Long
    lhWnd = FindWindow("Internet Explorer_TridentDlgFrame", vbNullString)
    If lhWnd <> 0 Then
        SendKeys "{ENTER}"
        Beep
    End If
End Sub
0
 

Author Comment

by:NessieB
ID: 13751460
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?
0
 
LVL 10

Expert Comment

by:edwardiii
ID: 13754845
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.
0
 

Author Comment

by:NessieB
ID: 13755701
Do the fact that I am using a webbrowser control make a difference?
0
 
LVL 10

Expert Comment

by:edwardiii
ID: 13756411
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?
0
 

Author Comment

by:NessieB
ID: 13761269
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
0
 
LVL 10

Expert Comment

by:edwardiii
ID: 13761663
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)...
0
 
LVL 10

Expert Comment

by:edwardiii
ID: 14332343
Thanks to PaulHews for this important Timer fact (from EE post: http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21473851.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).
0
 
LVL 32

Expert Comment

by:Erick37
ID: 14332407
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."
0
 
LVL 10

Expert Comment

by:edwardiii
ID: 14335467
Yikes! Sorry for missing that in your comments, Erick37. Thanks for the clarification:)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

850 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