timmytong

Hi all,

When I load a webpage in the webbrowser control, an alert popup and stop the program execution. How can I write code to close the alert

Here is a sample page:

<HTML>
<BODY>
<SCRIPT>alert('Testing');</SCRIPT>
</BODY>
</HTML>

Is there any way to close it on the Navigated or DocumentCompleted event of the webbrowser

Or is there any way to remove the alert script from the webpage before it's being displayed

Thanks a lot!




Re: Visual Basic General How can I close a javascript alert in WebBrowser

Bruno Yu - MSFT

timmytong,

According to your question on closing Javascript alert, I would like to provide you the suggestions as follows:

1. WebBrowser.Navigated Event occurs when the WebBrowser has navigated to a new document and has begun loading it while DocumentCompleted Event occurs when finishes loading a document. In my opinion, they are not good enough to close the Javascript alerts.

2. You can try to use SendKeys.Send method to send the "Enter" keystroke to close the alert automatically when the alert happens, the following code snippet shows you the way:

Code Block

' Use the SendKeys.Send method to raise the Button1 click event

' and display the message box.

Private Sub Form1_DoubleClick(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles MyBase.DoubleClick

' Send the enter key; since the tab stop of Button1 is 0, this

' will trigger the click event.

SendKeys.Send("{ENTER}")

End Sub

3. The following thread with the answers provided by Arun and anxvariety can help you on the issue:

I used HWND of the IE application and used GetLastActivePopup to identify the active pop window for IE application and used PostMessage to send ok to alert.

alerthwnd = GetLastActivePopup(iehwnd);
if (alerthwnd && (alerthwnd != iehwnd)) {
PostMessage(iehwnd, 0x113, 1, 0);
}

Anyone who is looking for a solution to this - I found a solution and thought I'd return here and post it, hopefully it helps someone!

See the following link:
http://vbcity.com/forums/topic.asp tid=108859

Hope that can help you.






Re: Visual Basic General How can I close a javascript alert in WebBrowser

timmytong

I can use the sendkeys.send("{ENTER}") to close the javascript alert. But how can I detect an alert is popup and automatically send the enter key any event fired

Thanks!






Re: Visual Basic General How can I close a javascript alert in WebBrowser

Bruno Yu - MSFT

timmytong,

I hope the following code snippet provided by Whatabohr can help you with the problem:

Code Block

<Runtime.InteropServices.ComVisible(True)> _

Public Class MyWindowExternal

Public Sub New()

End Sub

Public ReadOnly Property WebBrowserIdentifier() As String

Get

Return "TOKEN"

End Get

End Property

End Class

function goStart()

{

if(null != window.external.WebBrowserIdentifier && "TOKEN"==window.external.WebBrowserIdentifier) {

alert("In WinForms WebBrowser");

}

else {

alert("Not in WinForms WebBrowser");

}

}

For further information on the issue, please take a look at the following thread:

Web page detect if opened from WebBrowser control

Hope that can help you.