1

I have a problem with blocking iframes in the webbrowser control. Currently I am using this code:

foreach (HtmlElement x in ((WebBrowser)sender).Document.GetElementsByTagName("iframe"))
            {
                MessageBox.Show("iframe!"); //DEBUG
                x.OuterHtml = @"<iframe src=""about:blank"" frameborder=""0"" style=""display:none;""></iframe>";
//              x.OuterHtml = String.Empty; //gives the same result
            }

It works but when navigating to www.popuptest.com , the application just freezes completely because of this code. It shows 2 "iframe!" message boxes and freezes after closing the second one. I have found the 2 iframes in the source code of webpage (in the advertisements shown on that website). This is the code that is causing it to freeze:

(a=document.createElement("IFRAME"),a.frameBorder=0,a.style.height=0,a.style.width=0,a.style.position="absolute",t=a,document.body&&(document.body.appendChild(a),a=t))&&(a=a.contentWindow)&&(r="1",a.document.open(),a.document.write("<!doctype html><html><head></head><body></body></html>"),a.document.close(),k(a.document))

I guess it is because of the frame being created in a different way? I have tested it both on win7/IE10 and winXP/IE6 and the result is the same. On winXP, however, it crashes and opens the debugger instead of freezing and that is how I got the faulty code.

Is there a better/safer method of removing the content of iframes?

Kidades
  • 600
  • 2
  • 9
  • 26

1 Answers1

1

I would try disabling frames using Download Control (DLCTL_NO_FRAMEDOWNLOAD). Here's how it can possibly be done, although I haven't tried that myself. Let us know if that works for blocking frames or not.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • 2
    It is blocking iframes perfectly. :) However, all the other features are disabled such as DLIMAGES, VIDEOS and BGSOUNDS so you would need to add some of those too in addition to NO_FRAMEDOWNLOAD. The value I used was 4098 + 16 + 32 + 64 – Kidades Sep 09 '13 at 19:54