none
WPF SP1 WebBrowser - How To Stop Popups

    Question

  • Is there way to stop popups with the new WebBrowser control for WPF in SP1.

    The "Navigating" event is not called when a href with target="_blank" is clicked:

    <a href="http://www.example.com/" target="_blank">

    In my research I found that the old WebBrowser control generated a "NewWindow" event that could be cancelled. Is there something similar in the new control?

    Thanks
    Friday, June 06, 2008 6:41 PM

Answers

  • Unfortunately there is no good way to archive this right now. This is a good feedback. We will add it to the list of things to provide for the WebBrowser control in the future release.
    • Marked as answer by Marco Zhou Friday, June 13, 2008 9:43 AM
    Friday, June 06, 2008 9:56 PM
  • The best, I can propose in this case is to invoke user script to remove all _blank tagets (or, alternatively) change all targets to _top to make them be opened in the same window

    var d = document.getElementsByTagName('a');
    for (var i = d.length-1; i > -1; i--) {
    if (d.getAttribute('target') == "_blank") {
    d.removeAttribute('target');
    }
    }


    Tamir http://blogs.microsoft.co.il/blogs/tamir If your question was answered, please mark it.
    • Marked as answer by Marco Zhou Friday, June 13, 2008 9:43 AM
    Sunday, June 08, 2008 9:15 AM

All replies

  • Unfortunately there is no good way to archive this right now. This is a good feedback. We will add it to the list of things to provide for the WebBrowser control in the future release.
    • Marked as answer by Marco Zhou Friday, June 13, 2008 9:43 AM
    Friday, June 06, 2008 9:56 PM
  • Thanks for the response. At least I know now to stop looking.
    Saturday, June 07, 2008 12:24 AM
  • The best, I can propose in this case is to invoke user script to remove all _blank tagets (or, alternatively) change all targets to _top to make them be opened in the same window

    var d = document.getElementsByTagName('a');
    for (var i = d.length-1; i > -1; i--) {
    if (d.getAttribute('target') == "_blank") {
    d.removeAttribute('target');
    }
    }


    Tamir http://blogs.microsoft.co.il/blogs/tamir If your question was answered, please mark it.
    • Marked as answer by Marco Zhou Friday, June 13, 2008 9:43 AM
    Sunday, June 08, 2008 9:15 AM
  • Is this issue resolved in the 4.0 Framework?

    I also have a similar problem.

    Wednesday, April 07, 2010 1:27 PM
  • me too,do you resolved it in .net 4.0  ?

    i like “job”

    Saturday, June 30, 2012 1:56 PM
  • Hii tlanier,

    you can simply take your wpf web browser in WindowsFormsHost control and then in Constructor you can set the ScriptErrorsSuppressed property true for Blocking Pop ups .

    Saturday, July 21, 2012 5:59 AM
  • hi tlanier ,

    lucky is i use System.Windows.Controls.WebBrowser fixed it ,  this is code

     
     private void wb_Navigated(object sender, NavigationEventArgs e)
            {
                WebBrowser wb = sender as WebBrowser;
                var webBrowserHelper = new WebBrowserHelper(wb);
                HelperRegistery.SetHelperInstance(_container, webBrowserHelper);
                webBrowserHelper.NewWindow += WebBrowserOnNewWindow;
                SuppressScriptErrors(wb, true);
            }
    private void WebBrowserOnNewWindow(object sender, CancelEventArgs e)
            {
                try
                {
                    dynamic browser = sender;
                    dynamic activeElement = browser.Document.activeElement;
                    var link = activeElement.ToString();
                    // this is new Window,if you want open in current page,only change the browser.Source and Cancel the event
                    browser.Source = new Uri(link);
                }
                catch
                {
    
                }
                finally
                {
                    e.Cancel = true;
                }
            }


    i like “job”

    Tuesday, July 24, 2012 3:39 AM