none
How to Block Pop Up with WebBrowser Control RRS feed

  • Question

  • Hi guys, I'm trying to create a simple application using the WebBrowser control.

    When I visit a certain website using the webbrowser control, I noticed a pop up window opening. Is there anyway to block this pop up using the WebBrowser control?

    If so, can someone show me how. Also I'm new to C# .net, so it would be nice if someone can explain it to me in step by step process.

    Thanks!
    Monday, November 24, 2008 3:46 PM

Answers

  •  

    Hello,

            If here pop up means WebBrowser open a webpage in a new Internet Explorer, then please follow these steps to avoid such condition:

    1.       Click WebBrowser in Windows Form design time, and open property window of WebBrowser.

    2.       Add NewWindow event handler at Event tab of property window.

    3.       Use these codes in the event handler, like:

        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)

           {

               webBrowser1.Navigate(webBrowser1.StatusText);

               e.Cancel = true;

        }

    Here, StatusText actually holds the URL of the target webpage original opened in Internet Explorer.   Besides, e.Cancel = ture is to cancel the original action to open webpage in Internet Explorer.

     

            Otherwise, if you have any other concern, please be free to let me know.   

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Friday, November 28, 2008 2:11 PM
    Moderator

All replies

  •  

    Hello,

            If here pop up means WebBrowser open a webpage in a new Internet Explorer, then please follow these steps to avoid such condition:

    1.       Click WebBrowser in Windows Form design time, and open property window of WebBrowser.

    2.       Add NewWindow event handler at Event tab of property window.

    3.       Use these codes in the event handler, like:

        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)

           {

               webBrowser1.Navigate(webBrowser1.StatusText);

               e.Cancel = true;

        }

    Here, StatusText actually holds the URL of the target webpage original opened in Internet Explorer.   Besides, e.Cancel = ture is to cancel the original action to open webpage in Internet Explorer.

     

            Otherwise, if you have any other concern, please be free to let me know.   

     

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Friday, November 28, 2008 2:11 PM
    Moderator
  • Hi Lingzhi,

    Your code piece does wonderfully block the popups.

    I have a problem that a popup window with is blocked in IE is displayed as bar and thats it.

    When I implemented your code in my program it is throwing an error stating :
    A Runtime Error has occured.
    Do you wish to Debug ?
    
    Error: 'abc' is null or not an object.

    'Ok' 'Cancel'
    When I navigated to the Source of the page I found that abc was a Javascript function which is doing window.open.

    How can I smoothly (without any error messages) execute the webpage ?

    The problem here is that unless the Ok or the Cancel button is selected the program comes to a stand still.

    Regards,

    tvks

    Wednesday, July 1, 2009 11:13 PM
  • Change the WebBrowser property that shows javascript errors:

    webBrowser1.ScriptErrorsSuppressed = true;

    Regards

    Wednesday, July 8, 2009 8:32 PM
  • Hi Lingzhi,
                   I just read your reply but in my application i still have few pop up opening even after setting the e.cancel = true in the event handler. Can you suggest me some way to handle it. I guess may be they are due to usage of javascript in the origional webpage to create a pop up.

    Hope to hear from you or someone soon.

    Regards
    Ricky
    Thursday, November 26, 2009 10:07 AM
  • Hi, did you fix this problem?
    Friday, January 1, 2010 9:26 AM