最近在编写一个使用到TWebBrowser控件的软件,浏览网页时经常会弹出各种各样的窗口,尤其是广告,让人烦不胜烦,参考网上的一些资料,针对不同的弹窗方式采取相应的措施就能禁止各种弹窗。

1. 将TWebBrowser控件的Silent属性设置为True可以防止弹出“语法错误”的窗口。

2. 编写TWebBrowser控件的OnNewWindow2事件响应代码可以阻止<a href='url' target='_blank'>方式的新窗口

procedure TForm1.WebBrowser1NewWindow2(ASender: TObject; var ppDisp: IDispatch;
  var Cancel: WordBool);
begin
  Cancel := True;
end;


3. 编写TWebBrowser控件的OnNavigateComplete2事件响应代码可以阻止Javascript脚本弹出的新窗口

uses MsHTML;
procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
  ((aSender as TWebBrowser).Document as IHTMLDocument2).parentWindow.execScript('window.open=function(){return true}','JavaScript'); //禁止使用open方式弹出的窗口
  ((aSender as TWebBrowser).Document as IHTMLDocument2).parentWindow.execScript('window.alert=function(){return true}','JavaScript'); //禁止使用alert方式弹出的对话框
  ((aSender as TWebBrowser).Document as IHTMLDocument2).parentWindow.execScript('window.showModalDialog=function(){return true}','JavaScript'); //禁止使用showModalDialog方式弹出的窗口
  ((aSender as TWebBrowser).Document as IHTMLDocument2).parentWindow.execScript('window.showModelessDialog=function(){return true}','JavaScript'); //禁止使用showModelessDialog方式弹出的对话框
end;
posted on 2015-08-09 20:35  EEEEEEEEEEEEEEEEEEE  阅读(539)  评论(0编辑  收藏  举报