Stopping Popup Windows in A Web Browser

xiaoxiao2021-03-06  47

Edward

(vbcity leader)

posts: 1481 since: Apr 8, 2001 from: Shropshire, England http://www.vbcity.com/forums/topic.asp?tid=22075&highlight=beforenavigate&page=2 Hi, As most 'unwanted' pop-ups occur during the 'Online' Event of The Body Element, You Can Expand On Cancelling The NewWindow by Determining WHETER THE Document Being loaded HAS Completed: Code:

Private

SUB webbrowser1_newwindow2 (PPDISP

As Object, Cancel

AS

Boolean)

IF webbrowser1.document.readystate =

"interactive"

THEN

'Probable Script' Online '

Cancel =

True

Debug.print

"New window: blocked"

Else

'Check if an element has been actid

IF webbrowser1.document.activeElement

IS

Nothing

THEN

'Probable Script

Cancel =

True

Debug.print

"New window: blocked"

Else

'User Selection LIKELY

Debug.print

"New window: allowed"

End

IF

End

IF

End

Sub

Private

SUB FORM_LOAD ()

'EnSure the WebBrowser is Silent. Cancelled Pop-Ups Offen Throw A Script Error:

Webbrowser1.silent =

True

End

Sub

Hope That Helps I'm NOT Sure How You Can Get The NewWindow's Target Url withouting The New Window To Open.

One of the interesting things about the WebBrowser control is the number of ways you can achieve the same outcome. The expansion on the New Window routine is not elegant but hopefully it gives some indication of the range of possible reasons for a new window. The code will redirect any Target = "_ blank" type navigation to the original window, but will allow user-activated links that are script based to open in a new window - this would probably be the point at which to implement a new form and RegisterAsBrowser code. An Example Is The 'Comment' Link In A Microsoft Kb Article Which Uses JavaScript to Open The Target Window. Code: Private

SUB webbrowser1_newwindow2 (PPDISP

As Object, Cancel

AS

Boolean)

DIM SLINK

AS

String

On

Error

Resume

NEXT

IF

Not webbrowser1.document

IS

Nothing

THEN

IF

NOT webbrowser1.document.parentwindow.event

IS

Nothing

THEN

IF WebBrowser1.document.parentWindow.Event.

TYPE =

"Menuextunknown"

THEN

'Context Menu - NaviGate

'Get the url of the source element

Slink = webbrowser1.document.parentWindow.Event.srcelement.href

Debug.print

Context Menu: "& Slink

IF Len (Slink)> 0

THEN

'Cancel New Window

Cancel =

True

'Force Open in Current WINDOW

Webbrowser1.navigate Slink

End

IF

End

IF

Else

IF webbrowser1.document.activeElement

IS

Nothing

THEN

'Probable Online Script - Block

Debug.print

Probable Script: Unknown

Cancel =

True

Else

SLINK = webbrowser1.document.activeElement.href

IF Len (Slink)> 0

THEN

IF webbrowser1.document.activeElement.protocollong =

"Unknown Protocol"

THEN

'Probable scripted link - allow - *** SET New Form

Cancel = false

Else

'Link with External Target - NaviGate

Debug.print

"EXTERNAL LINK TARGET:" & SLINK

Cancel =

True

Webbrowser1.navigate Slink

End

IF

Else

'Unknown new window

Debug.print

"Unknown Reason: -"

Cancel =

True

End

IF

End

IF

End

IF

Else

'Probable Script - Block

Cancel =

True

End

IF

End

Sub

The Routine Checks The Protocol of The ActiveElelement's Link. Script based Navigation Will Return 'Unknown Protocol'.

Hope That Helps

转载请注明原文地址:https://www.9cbs.com/read-116211.html

New Post(0)