Programming magic, glory, and juices.

IWebBrowser2 Stopping and Disabling Popups

July 4th, 2006


With the WebBrowser control it is very difficult to host it and to stop all the different kinds of popups that can occur. All these methods took a huge amount of time to research and to develop.

Popups occuring through javascript when calling windows.open

This is probably the easily popup to handle. The DWebBrowserEvents2 interface has notifications that allow you to cancel popups that were created using windows.open. See notifications DISPID_NEWWINDOW2 and DISPID_NEWWINDOW3.

Popups are opened when a javascript contains windows.showModalDialog or window.showModallessDialog

<script>window.showModalDialog("http://www...", "", "");</script>

To stop this method of popup you can easily disable the javascript that causes it to happen. Setting the functions to null value in javascript will disable them. Use IHTMLWindow2::execScript during the NavigateComplete event to get the job done.

Popups are opened using javascript's alert function

To disable the javascript alert function you can set it's value to null like mentioned above. I also recommend checking for windows that popup in the current thread called "Microsoft Internet Explorer". These are alert window messages and should first be sent IDNO and then sent IDOK. Note that you cannot set alert to null but you have to set window.alert to null to disable the function.

Popups are opened when a page hosts a Windows Media Player object and uses javascript to redirect the user to the new page

<object id=iie width=0 height=0
classid='CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6'
style='display:none'></object>
<script>iie.launchURL(popURL);</script>

To prevent Windows Media Player from opening urls you have iterate through all the OBJECT elements and search for ones that look suspicious within DocumentComplete. Once you've found an object that is trying to open urls or that has a small size or that is not displayed, you can use IHTMLDocument->createElement to create a new OBJECT element and replace it with the old OBJECT element. When you create the new element be sure to use the same name attribute as the previous element and not to assign a classid to it. To replace the elements use IHTMLDOMNode::replaceNode.

If you try using IHTMLObjectElement2::put_classid to try and remove the classid of the OBJECT element it won't work. Best thing to do is to create an new OBJECT element with no classid and replace it with the old OBJECT element that contains the Windows Media Player classid.

Popups are opened via script errors that occur while using the WebBrowser control

One way you'll want to implement requires calling EnumWindows and iterating through all the open windows. You can compare the title of the windows with generic Microsoft Internet Explorer error windows and close any that you find match.

I also recommend implementing the IOleCommandTarget interface to remove any further popups. You can watch for the OLECMDID_SHOWSCRIPTERROR and OLECMDID_SHOWMESSAGE notifications and stop them before they occur. How to handle script errors as a WebBrowser control host.

Of course that method does not always work as Internet Explorer does not always send the OLECMDID_SHOWSCRIPTERROR notification. Microsoft provides a hack solution to try and resolve the issue. Script error notification is not sent to Exec method of WebBrowser host

Stopping popups using the WebBrower control's ambient properties

I do not use this method nor do I recommend this method. What some people have done is use the DISPID_AMBIENT_DLCONTROL notification to limit the functionality of the WebBrowser control. There really is no option that allows you to straight out limit popups. Whenever you try to use DLCTL_NO_SCRIPTS or DLCTL_NO_DLACTIVEXCTLS or any combinations of the flags that are available to you it always results in the final page rendering differently that it normally would. I do not recommend using the DISPID_AMBIENT_USERMODE notification either. With both of these the page will not render as it should and during some DWebBrowserEvents2 notifications you'll find you won't be able to access the document's body or the document itself or something like that.

Canceling file download windows

Sometimes Internet Explorer will try and download a file and thus display the file download dialog. You can stop this by handling the DISPID_FILEDOWNLOAD message in DWebBrowserEvents2.

An idea to stop popups using the browser UserAgent

You could always change the UserAgent so that it does not appear that you are using Internet Explorer or using Windows XP. I won't venture there because changing the UserAgent could affect the rendering of the page even though a lot of popup scripts do rely on the UserAgent to determine what methods it will try when opening a new window.

Resources
MSDN WebBrowser Customization
MSDN Reusing the WebBrowser Control
Google Groups - Problems Sinking and OnUnload
CodeProject - Popup Blocker

If I have skipped over a method or anything please let me know.

3 Responses to “IWebBrowser2 Stopping and Disabling Popups”
  1. Pingback:
    Re: Script error notification is not sent to Exec method of WebBrowser Host
  2. John
     

    Thanks for posting this. Big help today!

    Any suggestions on how I could use IHTMLWindow2::execScript to execute javascript found on a webpage and retrieve the value returned by the script?

  3. Eboni Rothfeld
     

    Hey, I came across this post while searching for help with JavaScript. I’ve recently changed browsers from Opera to IE. Just recently I seem to have a problem with loading JavaScript. Everytime I browse site that needs Javascript, the site doesn’t load and I get a “runtime error javascript.JSException: Unknown name”. I can’t seem to find out how to fix it. Any aid is greatly appreciated! Thanks

Leave a Reply