stopping popup windows in a web browser

原创 2004年09月16日 20:14:00

edward

(vbCity Leader)


Show this authors profile  Email the author of this post


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 'OnLoad' event of the Body element, you can expand on cancelling the NewWindow by determining whether 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 'OnLoad'
    Cancel = True
    Debug.Print "New Window: Blocked"
  Else
    ' Check if an element has been activated
    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 often throw a script error:
  WebBrowser1.Silent = True
End Sub



Hope that helps smile

I'm not sure how you can get the NewWindow's target URL without allowing 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 OnLoad 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 activeElement's link. Script based navigation will return 'Unknown Protocol'.

Hope that helps smile


Popup a Browser Window

 如果用户点击了一个按钮,并且你想弹出另外一个浏览器窗口,在SilverLight里面应该怎么做? SilverLight现在有个方法叫做 HtmlPage.PopupWindow(),由于安全原因,...
  • canduecho
  • canduecho
  • 2009年11月15日 15:21
  • 270

windows 子系统 linux (WLS):jupyter notebook 安装

1.确保已经安装了 python 和pip 2.运行: #pip install jupyter notebook 3.运行: #jupyter notebook 或者: #sud...
  • maoyongf2008
  • maoyongf2008
  • 2018年01月03日 17:45
  • 93

Google Chrome Your browser is not compatible with FlexPaper问题解决

如题,在用Google Chrome查看公司OA的公文时,发现浏览器加载不了公文内容,具体问题提示为: Your browser is not compatible with FlexPaper U...
  • jojoy_828
  • jojoy_828
  • 2017年12月14日 15:34
  • 638

Sorry, your browser / program is not supported by Web Dynpro

Sorry, your browser / program is not supported by Web Dynpro
  • champaignwolf
  • champaignwolf
  • 2017年10月30日 09:38
  • 460

zookeeper web ui-->node-zk-browser安装

目前公司正在使用zookeeper做配置管理和其他工作,在网上找几个zookeeper管理工具,都不尽人意,要么功能不够强大,要么不能友好的浏览zk树形结构,我的想法是zk管理工具,应该有一个树形结构...
  • lizhitao
  • lizhitao
  • 2014年06月15日 16:18
  • 9466

安装chromium web browser

在使用ubuntu12.04自带的firefox浏览器时,出现了
  • u012724595
  • u012724595
  • 2014年06月28日 22:05
  • 654

Web仿MSN弹出提示控件PopupWin无刷新弹出提示

网上关于PopupWin的使用方法有很多了,可以参考ASP.NET中Popup控件的使用方法。PopupWin的下载地址和英文原文可以访问以下地址:http://www.codeproject.com...
  • wang4978
  • wang4978
  • 2009年05月27日 14:34
  • 7596

could not launch external web browser for http://localhost:8080/spring_/.Check the Web Browser pre

这个工程不能用外部浏览器打开,有可能是安装包路径或者没有这个外部浏览器的安装包 **solution:windows->Preferences->General->WebBrowser**...
  • liqing110782
  • liqing110782
  • 2018年01月12日 08:46
  • 49

Problem 1414 - URL

Problem 1414 - URL Time Limit: 1000MS Memory Limit: 65536KB Difficulty: 3 Total Submit: 990 Ac...
  • u010924952
  • u010924952
  • 2015年08月26日 10:44
  • 343

Web Browser的网页中关闭Web Browser程序

在MSDN的论坛中看到这段代码, 自己试了一下有效, 记录下来namespace Sample1...{    [PermissionSet(SecurityAction.Demand, Name =...
  • zxkid
  • zxkid
  • 2007年06月29日 11:49
  • 736
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:stopping popup windows in a web browser
举报原因:
原因补充:

(最多只允许输入30个字)